AMADEUS html plain
This sketchy text is generated by AMADEUS
class BaseLanguageMatlab extends BaseLanguageJava
sama reduce
BaseLanguageMatlab()
public boolean sketchify(SketchyText skt0, SketchyText skt1,
AmFrame f)
System.out.println("BaseLang Matlab!!!!");
<======false
normaliseerimne (Java)
AmFrame frame;
public boolean normalize(SketchyText skt0,
main = source
SketchyText skt1,
main = new
AmFrame dialogTargetFrame)
frame = dialogTargetFrame;
skt1.main.insert(skt0.main.branch(0).makeCopy(),1);
skt1.main.removeMember(0);
skt1.main.baseLanguage = new BaseLanguageJava();
label = 0;
normalize(skt1.main);
<======true
normalize
Normaliseerida a'la JAVA
Va"limine skeem: lihtne, 1 lihtharuga;
pole va"limist katkestavaid nooli.
Haru:
lineaarne - ainult mittetriviaalse tekstiga 1 vo~i enam liiget
pseudolineaarne - nool viimasel kohal, eelmine osa tyhi
vo~i lineaarne
if0 - on va"hemalt 1 tingimus, nooli ei ole
if - on va"hemalt 1 tingimus ja veel yks nool viimasel kohal
no~rk nool katkestab vaid LOOP (ainukese) haru
pikendatud noole otsaskeemil on AMADEUSlabel.
private boolean normalize(Sketch s)
tagastab true, kui s eemaldatud vo~i avatud
skoopida tingimused
* int k = 0; k < s.body.size(); k++
Branch br = s.branch(k);
iga tingimuse (..nooleni) skoop teha lihtskeemils
scopeConditions(br);
* int k = 0; k < s.body.size(); k++
Branch br = s.branch(k);
* int i = 0; i < br.body.size(); i++
Object o = br.member(i);
? o instanceof Sketch
? normalize((Sketch)o)
i--;
? s.level() > 0
pole peaskeem
ehk saab avada vo~i eemaldada
String comm = s.comment.toString();
? (comm.length() >=2) &&
(comm.substring(0,2).compareTo("//") == 0)
on kommentaar, mis algab //
eemaldada see skeem
int i = s.parent.body.indexOf(s);
s.parent.body.removeElementAt(i);
<==========true
? s.type == Scheme.MODULE
? (s.head.size() == 0)
pa"iseta, avada lihtsalt
s.open();
<==========true
pa"iseta moodul
? s.primitiveHead(0).type == PrimitiveHead.LOOP
pa"ise tekst iga tekstiga-lihtprimitiivi ette
String htext = s.primitiveHead(0).text.toString();
Branch br = s.branch(0);
* int i = 0; i < br.body.size(); i++
Object o = br.member(i);
? o instanceof PrimitiveMember
PrimitiveMember p = (PrimitiveMember)o;
? (p.type == Primitive.SIMPLE)
? (!p.text.isTrivial())
* int j = 0; j < htext.length(); j++
p.text.row(0).insert(
new Symbol(htext.charAt(j)),
j+1);
s.head.removeAllElements();
s.open();
<======true
pa"isega moodul
moodul
pole peaskeem
skeem ja"i avamata
eemaldada tyhjad/semikoolon read
* int k = 0; k < s.body.size(); k++
Branch br = s.branch(k);
* int i = 0; i < br.body.size(); i++
Object o = br.member(i);
? o instanceof PrimitiveMember
PrimitiveMember p = (PrimitiveMember)o;
? (p.type == Primitive.SIMPLE)&&
(p.text.isTrivial())
peaks ka ";"
br.removeMember(i);
i--;
elimineerida saavutamatud osad
eliminateUnreachables(s);
iga pikendatud noole korral
lisada ma"rgend katkestatavale/ja"tkatavale
skeemile
no~rk pikendatud nool, mis ei ja"tka tsyklit,
==> tugev (pikem)
iga (liht)haru korral tyhi case --> default
iga (liht)haru korral tyhi catch --> finally
int sLevel = s.level();
* int k = 0; k < s.body.size(); k++
Branch br = s.branch(k);
case --> default, kui vaja
catch --> finally, kui vaja
? (br.type == Scheme.SIMPLE)
? (br.head.size() == 1)
PrimitiveHead he = br.primitiveHead(0);
String heStr = he.text.toString().trim();
? (he.type == PrimitiveHead.CASE) &&
(heStr.compareTo(":") == 0)
he.type = PrimitiveHead.DEFAULT;
<======
? (he.type == PrimitiveHead.CATCH) &&
(heStr.compareTo("") == 0)
he.type = PrimitiveHead.FINALLY;
* int i = 0; i < br.body.size(); i++
Object o = br.member(i);
? o instanceof Arrow
Arrow ar = (Arrow)o;
Scheme destin = ar.destination(br);
? destin instanceof Branch
no~rk nool
destin = destin.parent;
? destin.type != Scheme.LOOP
==> tugevaks
ar.type = Arrow.STRONG;
ar.level--;
? ar.level < sLevel
? (destin != null) &&
(destin.type != Scheme.MODULE)
lisada algusse ma"rgend
String dComment =
destin.comment.toString().trim();
? dComment.indexOf("[AMADEUSlabel:") < 0
kui veel pole
destin.comment = new Row(
"[AMADEUSlabel:" +
"L" + label +
"]" + dComment,
Default.commentColor
);
label++;
? destin.parent == null
AmDialog d =
new AmDialog
(frame, AmLocale.Error(),AmDialog.B_OK,
AmLocale.Normalize() + ":" +
AmLocale.Break_to_outer_level() + "!");
<======false
private void eliminateUnreachables(Sketch s)
* int k = 0; k < s.body.size(); k++
Branch br = s.branch(k);
* int i = 0; i < br.body.size() ; i++
Object o = br.member(i);
? o instanceof Arrow
? i != (br.body.size() - 1)
ei ole viimasel kohal
int j = i+1;
* ;j < br.body.size();
AmDialog d =
new AmDialog
(frame, AmLocale.Warning(),AmDialog.B_OK,
AmLocale.Normalize() + ":" +
AmLocale.Unreachables() + "!");
<==============
private void scopeConditions(Branch br)
* ;!conditionsScoped(br);
esimese tingimuse skoobi lo~petab nool
teha skoop esimesele tingimusele
LOOP:
* int i = 0; ; i++
Object o = br.member(i);
? o instanceof PrimitiveMember
PrimitiveMember p = (PrimitiveMember)o;
? p.type == Primitive.CONDITION
esimene tingimus
leida nool
* int j = i+1; ;j++
o = br.member(j);
? o instanceof Arrow
Arrow ar = (Arrow)o;
teha skoop
Selection sel =
new Selection(
new Cursor(br,p),
new Cursor(br,ar));
Sketch sk =
sel.convert2simple(0,0,new BaseLanguageJava());
scopeConditions(sk.branch(0));
<==========LOOP;
private boolean conditionsScoped(Branch b)
true, kui ei:
leidub tingimus ... nool, mis pole viimasel kohal
int i;
int n = b.body.size() - 1;
* i = 0; i < n; i++
Object o = b.member(i);
? (o instanceof PrimitiveMember)
PrimitiveMember p = (PrimitiveMember)o;
? (p.type == Primitive.CONDITION)
kas edasi tuleb veel nool
* i++; i < n; i++
o = b.member(i);
? (o instanceof Arrow)
<==============false
<======true
static int label;
kasut. nii nor,alize kui ka textualize
//
void reduce(Sketch s)
System.out.println("BaseLang Matlab!!!!");
tekstualiseerimne
Textualize ==================================================
static Branch br1;
static String indent =
" ";
static int currentLevel = 0;
static final int EMPTY = -2;
static final int NOT_NORMALIZED = -1;
static final int LINEAR = 0;
pole tingimusi ja nooli
static final int IF0 = 1;
on tingimusi, aga nooli ei ole
static final int IF = 2;
on tingimusi ja 1 nool lo~pus
static final int PSEUDOLINEAR = 3;
pole tingimusi, on nooli
textualize
static final int METHOD = 3;
static final int LOOP = 4;
static final int SWITCH = 5;
static final int TRY = 6;
public boolean textualize(SketchyText skt0,
main = source
SketchyText skt1,
main = new
AmFrame dialogTargetFrame)
frame = dialogTargetFrame;
Sketch sNew = skt1.main;
Sketch s = skt0.main.makeCopy();
label = 0;
//normalize(s);
sNew.baseLanguage = new BaseLanguageMatlab();
sNew.branch(0).baseLanguage = new BaseLanguageMatlab();
teha s-st read ja lisada need uue haru primtiivideks
Branch br = s.branch(0);
br1 = sNew.branch(0);
br1.body.removeElementAt(0);
insertLine("% This Matlab-text is generated by AMADEUS");
* int i = 0; i < br.body.size(); i++
Object o = br.member(i);
Matlab korral o: peaks olema skeem (module)
? o instanceof PrimitiveMember
textualize((PrimitiveMember)o);
siiski
<------
? o instanceof Sketch
textualize((Sketch)o);
<======true
private void textualize(Primitive p)
lihtliige
? p.text.isTrivial()
<======
String rida = "";
//
* int i = 0; i < p.text.rows.size(); i++
primitiivi
p.text iga rea korral
rida = rida + p.text.row(i).toString());
insertLine(p.text.toString());
private void textualizeCondition(Primitive p)
String tingimus = "if ";
* int i = 0; i < p.text.rows.size(); i++
ühte ritta
p.text iga rea korral
tingimus = tingimus + p.text.row(i).toString();
insertLine(tingimus);
private void textualize(PrimitiveHead he)
String rida = "";
lihtpa"is
String pref = "";
String suff = "";
??he.type
? PrimitiveHead.SIMPLE:
<======
? PrimitiveHead.LOOP:
pref = "for "; suff = " ";
<======
? PrimitiveHead.SWITCH:
pref = "switch "; suff = " ";
<======
Mathlabis ei ole ?? while
? PrimitiveHead.TRY:
po midagi muud
<======
? PrimitiveHead.DEFAULT:
? PrimitiveHead.CASE:
pref = "case ";
String heText = he.text.toString().trim();
? heText.length() == 0
pref = "otherwise ";
<======
Mathlabis on ka
? PrimitiveHead.CATCH:
<======
Mathlabis ei ole
? PrimitiveHead.FINALLY:
<======
? pref.length() > 0
rida = pref + rida;
* int i = 0; i < he.text.rows.size(); i++
POOLITAMINE ??? ...EI!
he.text iga rea korral
rida = rida + he.text.row(i).toString();
? suff.length() > 0
rida = rida + suff;
insertLine(rida);
private void insertLine(String str)
str = indent.substring(
0, Math.min(indent.length(),
currentLevel+1))
+ str;
Text newT = new Text();
newT.rows.removeElementAt(0);
newT.rows.addElement(
new Row(str, Default.foregroundColor));
br1.body.addElement(
new PrimitiveMember(Primitive.SIMPLE, newT));
private void textualize(Sketch sk)
currentLevel++;
kui vaja, siis ma"rgend
pa"ised la"hevad lihtprimitiividena
boolean isFunction = false;
boolean isSwitch = false;
boolean isHeadLoop = false;
boolean isFreeLoop = false;
int i;
päis
? (sk.type == Scheme.LOOP) && (sk.head.size() == 0)
isFreeLoop = true;
insertLine("while true ");
???
<======
? (sk.type == Scheme.MODULE) && (sk.head.size() == 1)
isFunction = true;
textualize(sk.primitiveHead(0));
<======
? (sk.type == Scheme.SIMPLE) &&
(sk.head.size() == 1) &&
sk.primitiveHead(0).type == PrimitiveHead.SWITCH
isSwitch = true;
textualize(sk.primitiveHead(0));
<======
? (sk.type == Scheme.LOOP) && (sk.head.size() > 0)
isHeadLoop = true;
päis(t)ega tsükkel
* i = 0; i < sk.head.size(); i++
textualize(sk.primitiveHead(i));
keha
* i = 0; i < sk.body.size(); i++
textualize(sk.branch(i));
lõpud
<======
? isSwitch
insertLine("end % switch");
<======
? isFreeLoop
insertLine("end % free loop");
<======
? isHeadLoop
* int hi = 0; hi < sk.head.size(); hi++
insertLine("end % head loop");
currentLevel--;
private void textualize(Branch b)
kui on kaheharulise moodulskeemi 1. haru
Sketch parent = (Sketch)(b. parent);
? parent.type == Scheme.MODULE
? parent.body.size() == 2
? b == parent.branch(0)
String txt = "";
? b.head.size() == 1
insertLine('%' + b.primitiveHead(0).text.toString());
* int i = 0; i < b.body.size(); i++
PrimitiveMember p = (PrimitiveMember)(b.member(i));
insertLine('%' + p.text.toString());
insertLine(" ");
<==========
int conditionsOpenedCount = 0;
silum:
String sss = b.comment.toString();
currentLevel++;
pa"ised (case korral vo~ib olla mitu)
boolean isCase = false;
int i;
* i = 0; i < b.head.size(); i++
? (b.primitiveHead(i).type == PrimitiveHead.CASE) ||
(b.primitiveHead(i).type == PrimitiveHead.DEFAULT)
isCase = true;
textualize(b.primitiveHead(i));
//Matlab
? !isCase
insertLine("{");
??branchBodyKind(b)
? EMPTY:
0 vo~i enam paljaid kommentaare
<======
ei tee ridu
? LINEAR:
* i = 0; i < b.body.size(); i++
Object o = b.member(i);
? o instanceof Sketch
textualize((Sketch)o);
<------
textualize((PrimitiveMember)o);
<======
? :
* i = 0; i < b.body.size(); i++
Object o = b.member(i);
? o instanceof Sketch
textualize((Sketch)o);
<------
PrimitiveMember p = (PrimitiveMember)o;
? p instanceof Arrow
* ; conditionsOpenedCount > 1;
currentLevel--;
insertLine("end % if");
conditionsOpenedCount--;
currentLevel--;
insertLine("else ");
currentLevel++;
<------
? p.type == Primitive.SIMPLE
textualize(p);
<------
? p.type == Primitive.CONDITION
textualizeCondition(p);
conditionsOpenedCount++;
currentLevel++;
* ; conditionsOpenedCount > 0;
currentLevel--;
insertLine("end % if");
conditionsOpenedCount--;
//
??branchBodyKind(b)
? EMPTY:
0 vo~i enam paljaid kommentaare
<======
ei tee ridu
? LINEAR:
* i = 0; i < b.body.size(); i++
Object o = b.member(i);
? o instanceof Sketch
textualize((Sketch)o);
<----------------------
textualize((PrimitiveMember)o);
<======================
? IF0:
1 vo~i enam lahtist tingimust
* i = 0; i < b.body.size(); i++
Object o = b.member(i);
? o instanceof Sketch
textualize((Sketch)o);
<------
PrimitiveMember p = (PrimitiveMember)o;
? p.type == Primitive.SIMPLE
textualize(p);
<------
p.type == Primitive.CONDITION
textualizeCondition(p);
conditionsOpenedCount++;
String elsebreak = "";
? (b.parent.type == Scheme.LOOP) &&
b.parent.isHeadless()
pa"iseta tsyklis
elsebreak = "else break;";
tingimused kinni
* int ii = 0; ii < conditionsOpenedCount; ii++
//insertLine("}"+elsebreak);
insertLine("END");
<======
? IF:
on tingimusi, parajasti yks nool lo~pus
? PSEUDOLINEAR:
pole tingimusi, on nooli
conditionsOpenedCount = 0;
* i = 0; i < b.body.size(); i++
Object o = b.member(i);
? o instanceof Sketch
textualize((Sketch)o);
<------
? o instanceof Arrow
Arrow ar = (Arrow)o;
int len = b.level() - ar.level;
mittetavaline asendub
break/continue/return
? (b.head.size() > 0) &&
(((b.primitiveHead(0).type == PrimitiveHead.CASE) ||
(b.primitiveHead(0).type == PrimitiveHead.DEFAULT)) &&
(len == 1))
case--break
insertLine("break;");
<--------------
? (b.parent.type == Scheme.LOOP) &&
(len <= 1)
haru LOOP-is
tsyklis
? len == 0
insertLine("continue;");
<======
? len == 1
insertLine("break;");
<--------------
? (len == 1) && (b.parent.type == Scheme.MODULE)
lyhike return MODULE-s
textualizeReturn(ar);
<--------------
? len > 1
leida skeem, milleni ulatub
Scheme x = ar.destination(b);
? x instanceof Branch
x = x.parent;
? x.type == Scheme.MODULE
textualizeReturn(ar);
<======
String str = x.comment.toString();
str = " " +
str.substring(14,
str.indexOf("]"));
? ar.type == Arrow.STRONG
insertLine("break" +
str +
";");
<======
insertLine("continue" +
str +
";");
<======
???
tavaline nool:
else ei tohi olla !
<==============
???
PrimitiveMember p = (PrimitiveMember)o;
? p.type == Primitive.SIMPLE
? !p.text.isTrivial()
if(conditionsOpenedCount == 0){
algas jrk osa peale noolt
insertLine("{");
conditionsOpenedCount = 1;
lastOpenedConditionIsReal = false;
}
textualize(p);
<------
???
if(p.type == Primitive.CONDITION)
textualizeCondition(p);
controlsCount++;
conditionsOpenedCount++;
lastOpenedConditionIsReal = true;
tingimused kinni
* int ii = 0; ii < conditionsOpenedCount; ii++
<======
..default
? :
<==========
???
//
? !isCase
insertLine("}");
currentLevel--;
// see meetod on juba normaliseerimisel
private boolean conditionsScoped(Branch b)
true, kui ei:
leidub tingimus ... nool, mis pole viimasel kohal
int i;
int n = b.body.size() - 1;
* i = 0; i < n; i++
Object o = b.member(i);
? o instanceof PrimitiveMember
PrimitiveMember p = (PrimitiveMember)o;
? p.type == Primitive.CONDITION
kas edasi tuleb veel nool
* i++; i < n; i++
o = b.member(i);
? o instanceof Arrow
<==============false
<======true
private int branchBodyKind(Branch b)
int i;
int nontrivialsCount = 0;
int arrowsCount = 0;
int conditionsCount = 0;
* i = 0; i < b.body.size(); i++
Object o = b.member(i);
? o instanceof Sketch
nontrivialsCount++;
<------
? o instanceof Arrow
//
? (conditionsCount > 0) &&
(i != b.body.size() - 1)
<==============NOT_NORMALIZED
nontrivialsCount++;
arrowsCount++;
<------
PrimitiveMember p = (PrimitiveMember)o;
? p.type == Primitive.CONDITION
nontrivialsCount++;
conditionsCount++;
<------
<==========NOT_NORMALIZED
on mittetriviaalne lihtliige
nontrivialsCount++;
? nontrivialsCount == 0
<======EMPTY
? (arrowsCount == 0) && (conditionsCount == 0)
<======LINEAR
? (conditionsCount == 0) && (arrowsCount >0)
<======PSEUDOLINEAR
? (arrowsCount == 0) && (conditionsCount > 0)
<======IF0
<======IF
void prepareTex(Sketch s)
<======
byte nextPrimitiveHeadType(Scheme s, byte prev)
praegu võetud BaseLanguge0 -st
yledefineeritud
s - skeem, mille pa"is, prev - pa"ise tyyp
? (s instanceof Sketch)
??prev
? PrimitiveHead.SIMPLE:
<==============PrimitiveHead.LOOP
<==============PrimitiveHead.SWITCH
<==============PrimitiveHead.TRY
<==============PrimitiveHead.SIMPLE
<======
Branch haru korral lubab koiki pa"ise tyype
soltumata skeemi pa"isest
??prev
? PrimitiveHead.SIMPLE:
<==============PrimitiveHead.LOOP
<==============PrimitiveHead.SWITCH
<==============PrimitiveHead.CASE
<==============PrimitiveHead.DEFAULT
<==============PrimitiveHead.TRY
<==============PrimitiveHead.CATCH
..case
? PrimitiveHead.CATCH:
<==============PrimitiveHead.FINALLY
<==============PrimitiveHead.SIMPLE
<======prev