public void add(Var e) { int index = vindex++; e.setIndex(index); vhash.Add(e.getName(), e); vhash.Add(index, e); }
public void parse_and_compile() { prolog(); tree = new VarList(); IDictionaryEnumerator libEnum = lib.get_enum(); while ( libEnum.MoveNext() ) { Var procvar = new Var(); LibFunc lfunc = (LibFunc)libEnum.Value; procvar.setName(lfunc.nameShort); procvar.setType(Var.VAR_BLOCK); procvar.setTypeId(Tok.TOK_VOID); procvar.nodes = new VarList(); for (int i = 0; i < lfunc.typeParams.Count; i++ ) { Var param = new Var(); param.setName("PAR_"+i); param.setType(Var.VAR_PARAM); param.setTypeId((int)lfunc.typeParams[i]); procvar.nodes.add(param); } tree.add(procvar); } io.ReadChar(); tok.scan(); declarations(tree); while (tok.NotEOF()) { stmt(tree, null, null); } io.Message("compiled successfuly"); io.TreeDraw(tree); epilog(); }
void proc_decl(VarList curtree, String label) { Var procvar = new Var(); procvar.setName(label); procvar.setType(Var.VAR_BLOCK); procvar.setTypeId(Tok.TOK_VOID); if (label == null) io.Abort("PL0116: PROC declaration needs LABEL"); io.Message(tok+"[PROC]"); tok.scan(); procvar.nodes = new VarList(); if (tok.getId() == Tok.TOK_1_LBRACKET) { param(procvar.nodes); procvar.setNodesType(Var.VAR_PARAM); } switch (tok.getId()) { case Tok.TOK_RETURNS: io.Message(tok+"[RETURNS]"); tok.scan(); if (tok.getId() != Tok.TOK_1_LBRACKET) io.Abort("PL0104: ')' expected."); io.Message(tok+"[(]"); tok.scan(); switch (tok.getId()) { case Tok.TOK_FIXED: case Tok.TOK_FLOAT: case Tok.TOK_COMPLEX: case Tok.TOK_REAL: case Tok.TOK_BINARY: case Tok.TOK_DECIMAL: io.Message(tok+"[Type]"); procvar.setTypeId(tok.getId()); break; default: io.Abort("PL0115: type specifier expected"); break; } tok.scan(); if (tok.getId() != Tok.TOK_1_RBRACKET) io.Abort("PL0114: ')' expected"); io.Message(tok+"[)]"); tok.scan(); break; case Tok.TOK_RECURSIVE: io.Message(tok+"[RECURSIVE]"); if (label.ToUpper() == "MAIN") io.Abort("PL0146: MAIN can not be RECURSIVE"); tok.scan(); break; } null_stmt(); emit.FuncBegin(procvar); declarations(procvar.nodes); VarList prms = procvar.getParams(); if (prms != null) for (int i = 0; i < prms.Length(); i++) if (prms.FindByIndex(i).getTypeId() == 0) io.Abort("PL0117: undeclared parameter"); if (procvar.getLocals() != null) { emit.LocalVars(procvar.getLocals()); } try { curtree.add(procvar); } catch { io.Abort("PL0120: invalid procedure declaration"); } do { stmt(procvar.nodes, label, null); } while (tok.getId() != Tok.TOK_END); if (tok.getId() != Tok.TOK_END) io.Abort("PL0118: END expected"); io.Message(tok+"[END]"); tok.scan(); if (tok.getValue() != label) io.Abort("PL0119: unclosed PROC"); io.Message(tok+"[Label]"); tok.scan(); emit.Ret(); emit.FuncEnd(); if (io.getGenList()) emit.LIST(); emit.IL(); emit.Finish(); }
void label(Tok s) { Var var = new Var(); String label1 = "L@@"+s.getValue(); io.Message(s+"[Label]"); var.setName(label1); var.setType(Var.VAR_LABEL); try { currenttree.add(var); } catch { io.Abort("PL0111: invalid label declaration"); } if (tok.getId() != Tok.TOK_1_REL) io.Abort("PL0112: ':' expected"); io.Message(tok+"[:]"); last_label = s.getValue(); tok.scan(); }
void ident_stmt(VarList curtree) { Tok s = new Tok(null); VarList vars = new VarList(); Var V; int gotType = Tok.TOK_BINARY; s.setValue(tok.getValue()); s.setId(tok.getId()); tok.scan(); switch (tok.getId()) { case Tok.TOK_1_EQUALS: if (curtree.FindByName(s.getValue()) == null) { io.Abort("PL0137: undeclared variable"); } V = new Var(); V.setName(s.getValue()); vars.add(V); io.Message(s + "[Variable]"); io.Message(tok + "[Equals]"); tok.scan(); gotType = bool_expr(0); typeCheckAssign(gotType, curtree.FindByName(vars.FindByIndex(0).getName()).getTypeId()); null_stmt(); break; case Tok.TOK_1_COMMA: io.Message(s + "[Variable]"); if (curtree.FindByName(s.getValue()) == null) { io.Abort("PL0137: undeclared variable"); } V = new Var(); V.setName(s.getValue()); vars.add(V); while (tok.getId() != Tok.TOK_1_EQUALS) { if (tok.getFirstChar() != ',') { io.Abort("PL0138: ',' or '=' expected"); } io.Message(tok + "[Comma]"); tok.scan(); if (tok.getId() != Tok.TOK_IDENT) { io.Abort("PL0139: ident expected"); } if (curtree.FindByName(tok.getValue()) == null) { io.Abort("PL0137: undeclared variable"); } V = new Var(); V.setName(tok.getValue()); vars.add(V); io.Message(tok + "[Variable]"); tok.scan(); } io.Message(tok + "[Assign]"); tok.scan(); gotType = bool_expr(0); typeCheckAssign(gotType, curtree.FindByName(vars.FindByIndex(0).getName()).getTypeId()); null_stmt(); break; case Tok.TOK_1_REL: label(s); break; default: io.Abort("PL0140: not found expected token ':', ',' or '='"); break; } for (int i = 0; i < vars.Length(); i++) { typeCheckAssign(gotType, curtree.FindByName(vars.FindByIndex(i).getName()).getTypeId()); emit.Store(curtree.FindByName(vars.FindByIndex(i).getName())); if (i < vars.Length() - 1) { emit.Load(curtree.FindByName(vars.FindByIndex(0).getName())); } } }
private String genDataTypeSig(Var e) { if (e == null) return null; StringBuilder sb = new StringBuilder(Io.MAXSTR); if (e.getSign() == Tok.T_UNSIGNED) sb.Append("unsigned "); sb.Append(ilSType(e.getTypeId())); return (sb.ToString()); }
public void FieldDef(Var e) { NextInsn(0); icur.setIType(IAsm.I_FIELD); icur.setVar(e); }
public void Store(Var e) { NextInsn(1); icur.setIType(IAsm.I_INSN_STORE); icur.setVar(e); }
public void FuncBegin(Var e) { NextInsn(0); icur.setIType(IAsm.I_FUNC_BEGIN); icur.setVar(e); }
public void setVar(Var v) { ivar = v; }
void genLoad(Var e) { int id = e.getClassId(); if (e == null) io.ICE("load instruction with no variable ptr"); if (e.getLocalToken() != null) { // LocalToken lt = (LocalToken) e.getLocalToken(); LocalBuilder lt = (LocalBuilder) e.getLocalToken(); il.Emit(OpCodes.Ldloc, lt); } else { if (e.getFieldBuilder() != null) { FieldBuilder fb = (FieldBuilder) e.getFieldBuilder(); if (id == Tok.T_STATIC) il.Emit(OpCodes.Ldsfld, fb); else il.Emit(OpCodes.Ldfld, fb); } else { int index = e.getIndex(); if (id == Tok.T_PARAM) { if (index <= 256) il.Emit(OpCodes.Ldarg_S, index); else il.Emit(OpCodes.Ldarg, index); } else { if (id == Tok.T_AUTO || id == Tok.T_DEFCLASS) { if (index <= 256) il.Emit(OpCodes.Ldloc_S, e.getIndex()); else il.Emit(OpCodes.Ldloc, e.getIndex()); } else { io.ICE("instruction load of unknown class (" + e.getClassId()+")"); } } } } }
/* common routine to construct a signature string for a given varlist item requires a destination ptr, will return the updated dest ptr */ private Type genDataTypeSig(Var e) { bool sign = true; if (e == null) return null; if (e.getSign() == Tok.T_UNSIGNED) /* if var is unsigned, put it in sig */ sign = false; Type sig = ilSType(sign, e.getTypeId()); /* get the datatype */ return (sig); }
void do_stmt(VarList curtree) { bool needBranch = true; String label1 = new_label(); // loop start String label2 = new_label(); // loop end String label3 = new_label(); String label4 = new_label(); Var procvar = new Var(); procvar.setName(curtree.genName()); procvar.setType(Var.VAR_BLOCK); procvar.nodes = new VarList(); for (int i = 0; i < curtree.Length(); i++) { if ((curtree.FindByIndex(i).type & (Var.VAR_LOCAL | Var.VAR_PARAM | Var.VAR_LABEL)) != 0) { procvar.add(curtree.FindByIndex(i)); } } io.Message(tok + "[DO]"); tok.scan(); switch (tok.getId()) { case Tok.TOK_WHILE: io.Message(tok + "[WhileStatement]"); emit.Label(label1); tok.scan(); bool_expr(0); null_stmt(); emit.Branch("brfalse", label2); break; case Tok.TOK_IDENT: // TODO assign_stmt(curtree); if (tok.getId() != Tok.TOK_TO) { io.Abort("PL0123: TO expected"); } tok.scan(); bool_expr(0); null_stmt(); break; case Tok.TOK_CASE: io.Message(tok + "[CaseStatement]"); tok.scan(); break; case Tok.TOK_1_SEMI: io.Message(tok + "[;]"); needBranch = false; tok.scan(); break; } do { stmt(procvar.nodes, label2, label1); } while (tok.getId() != Tok.TOK_END); if (needBranch) { emit.Branch("br", label1); } if (tok.getId() != Tok.TOK_END) { io.Abort("PL0124: END expected"); } io.Message(tok + "[END]"); tok.scan(); curtree.add(procvar); if (needBranch) { emit.Label(label2); } }
void proc_decl(VarList curtree, String label) { Var procvar = new Var(); procvar.setName(label); procvar.setType(Var.VAR_BLOCK); procvar.setTypeId(Tok.TOK_VOID); if (label == null) { io.Abort("PL0116: PROC declaration needs LABEL"); } io.Message(tok + "[PROC]"); tok.scan(); procvar.nodes = new VarList(); if (tok.getId() == Tok.TOK_1_LBRACKET) { param(procvar.nodes); procvar.setNodesType(Var.VAR_PARAM); } switch (tok.getId()) { case Tok.TOK_RETURNS: io.Message(tok + "[RETURNS]"); tok.scan(); if (tok.getId() != Tok.TOK_1_LBRACKET) { io.Abort("PL0104: ')' expected."); } io.Message(tok + "[(]"); tok.scan(); switch (tok.getId()) { case Tok.TOK_FIXED: case Tok.TOK_FLOAT: case Tok.TOK_COMPLEX: case Tok.TOK_REAL: case Tok.TOK_BINARY: case Tok.TOK_DECIMAL: io.Message(tok + "[Type]"); procvar.setTypeId(tok.getId()); break; default: io.Abort("PL0115: type specifier expected"); break; } tok.scan(); if (tok.getId() != Tok.TOK_1_RBRACKET) { io.Abort("PL0114: ')' expected"); } io.Message(tok + "[)]"); tok.scan(); break; case Tok.TOK_RECURSIVE: io.Message(tok + "[RECURSIVE]"); if (label.ToUpper() == "MAIN") { io.Abort("PL0146: MAIN can not be RECURSIVE"); } tok.scan(); break; } null_stmt(); emit.FuncBegin(procvar); declarations(procvar.nodes); VarList prms = procvar.getParams(); if (prms != null) { for (int i = 0; i < prms.Length(); i++) { if (prms.FindByIndex(i).getTypeId() == 0) { io.Abort("PL0117: undeclared parameter"); } } } if (procvar.getLocals() != null) { emit.LocalVars(procvar.getLocals()); } try { curtree.add(procvar); } catch { io.Abort("PL0120: invalid procedure declaration"); } do { stmt(procvar.nodes, label, null); } while (tok.getId() != Tok.TOK_END); if (tok.getId() != Tok.TOK_END) { io.Abort("PL0118: END expected"); } io.Message(tok + "[END]"); tok.scan(); if (tok.getValue() != label) { io.Abort("PL0119: unclosed PROC"); } io.Message(tok + "[Label]"); tok.scan(); emit.Ret(); emit.FuncEnd(); if (io.getGenList()) { emit.LIST(); } emit.IL(); emit.Finish(); }
void stmt(VarList curtree, String ilabel, String olabel) { Var e = new Var(); currenttree = curtree; if (io.EOF()) { io.Abort("PL0142: expected statement but end of file encountered"); } if ((tok.getId() != Tok.TOK_PROC) && (tok.getId() != Tok.TOK_PROCEDURE)) { if (last_label != null) { emit.Label("L@@" + last_label); last_label = null; } } switch (tok.getId()) { case Tok.TOK_RETURN: ret_stmt(ilabel, olabel); null_stmt(); break; case Tok.TOK_IDENT: ident_stmt(curtree); break; case Tok.TOK_CALL: call_stmt(curtree); null_stmt(); break; case Tok.TOK_DO: do_stmt(curtree); null_stmt(); break; case Tok.TOK_GO: case Tok.TOK_GOTO: goto_stmt(curtree); null_stmt(); break; case Tok.TOK_PROCEDURE: case Tok.TOK_PROC: proc_decl(curtree, last_label); null_stmt(); break; case Tok.TOK_IF: if_stmt(curtree, ilabel, olabel); break; case Tok.TOK_DCL: case Tok.TOK_DECLARE: io.Abort("PL0143: wild declaration found"); break; case Tok.TOK_UNKNOWN: io.Abort("PL0144: unknown construction"); break; case Tok.TOK_END: case Tok.TOK_ELSE: case Tok.TOK_THEN: break; default: io.Abort("PL0145: wild symbol found"); break; } }
void simple_var(VarList curtree) { Var var = new Var(); var.setName(tok.getValue()); var.setClassId(Tok.T_DEFCLASS); try { curtree.add(var); } catch { io.Abort("PL0107: invalid variable declaration"); } io.Message(tok+"[Var]"); tok.scan(); }
void stmt(VarList curtree, String ilabel, String olabel) { Var e = new Var(); currenttree = curtree; if (io.EOF()) io.Abort("PL0142: expected statement but end of file encountered"); if ((tok.getId() != Tok.TOK_PROC) && (tok.getId() != Tok.TOK_PROCEDURE)) if (last_label != null) { emit.Label("L@@"+last_label); last_label = null; } switch (tok.getId()) { case Tok.TOK_RETURN: ret_stmt(ilabel, olabel); null_stmt(); break; case Tok.TOK_IDENT: ident_stmt(curtree); break; case Tok.TOK_CALL: call_stmt(curtree); null_stmt(); break; case Tok.TOK_DO: do_stmt(curtree); null_stmt(); break; case Tok.TOK_GO: case Tok.TOK_GOTO: goto_stmt(curtree); null_stmt(); break; case Tok.TOK_PROCEDURE: case Tok.TOK_PROC: proc_decl(curtree, last_label); null_stmt(); break; case Tok.TOK_IF: if_stmt(curtree, ilabel, olabel); break; case Tok.TOK_DCL: case Tok.TOK_DECLARE: io.Abort("PL0143: wild declaration found"); break; case Tok.TOK_UNKNOWN: io.Abort("PL0144: unknown construction"); break; case Tok.TOK_END: case Tok.TOK_ELSE: case Tok.TOK_THEN: break; default: io.Abort("PL0145: wild symbol found"); break; } }
void var_list(VarList curtree) { Var var1 = new Var(); var1.setName(tok.getValue()); try { curtree.add(var1); } catch { io.Abort("PL0107: invalid variable declaration"); } io.Message(tok+"[Var]"); tok.scan(); while ((tok.getId() == Tok.TOK_1_COMMA) || (tok.getId() == Tok.TOK_IDENT)) { if (tok.getFirstChar() != ',') io.Abort("PL0108: ',' or ')' expected"); io.Message(tok+"[Comma]"); tok.scan(); if (tok.getId() != Tok.TOK_IDENT) io.Abort("PL0109: ident expected"); var1 = new Var(); var1.setName(tok.getValue()); try { curtree.add(var1); } catch { io.Abort("PL0107: invalid variable declaration"); } io.Message(tok+"[Var]"); tok.scan(); } }
public void Load(Var e) { NextInsn(1); icur.setIType(IAsm.I_INSN_LOAD); icur.setVar(e); }
Var call_construct(string s) { Var procvar = new Var(); Var e; int i = 0; int parType = Tok.TOK_BINARY; e = tree.FindByName(s); if (e == null) io.Abort("PL0132: invalid procedure call: undefined procedure"); if (e.getType() != Var.VAR_BLOCK) io.Abort("PL0133: invalid procedure call: not a procedure"); tok.scan(); if (tok.getId() != Tok.TOK_1_LBRACKET) io.Abort("PL0134: '(' expected"); tok.scan(); if (tok.getId() != Tok.TOK_1_RBRACKET) { parType = bool_expr(0); typeCheckAssign(parType, e.getParams().FindByIndex(i).getTypeId()); i ++; while (tok.getId() == Tok.TOK_1_COMMA) { tok.scan(); parType = bool_expr(0); typeCheckAssign(parType, e.getParams().FindByIndex(i).getTypeId()); i ++; } if (tok.getId() != Tok.TOK_1_RBRACKET) io.Abort("PL0135: ')' expected"); } if (i != e.getParams().Length()) io.Abort("PL0136: invalid procedure call: wrong number of parametrs"); return e; }
public void Call(Var e) { NextInsn(1); icur.setIType(IAsm.I_CALL); icur.setVar(e); }
void do_stmt(VarList curtree) { bool needBranch = true; String label1 = new_label(); // loop start String label2 = new_label(); // loop end String label3 = new_label(); String label4 = new_label(); Var procvar = new Var(); procvar.setName(curtree.genName()); procvar.setType(Var.VAR_BLOCK); procvar.nodes = new VarList(); for (int i=0; i<curtree.Length(); i++) if ((curtree.FindByIndex(i).type & (Var.VAR_LOCAL|Var.VAR_PARAM|Var.VAR_LABEL)) != 0) procvar.add(curtree.FindByIndex(i)); io.Message(tok+"[DO]"); tok.scan(); switch (tok.getId()) { case Tok.TOK_WHILE: io.Message(tok+"[WhileStatement]"); emit.Label(label1); tok.scan(); bool_expr(0); null_stmt(); emit.Branch("brfalse", label2); break; case Tok.TOK_IDENT: // TODO assign_stmt(curtree); if (tok.getId() != Tok.TOK_TO) io.Abort("PL0123: TO expected"); tok.scan(); bool_expr(0); null_stmt(); break; case Tok.TOK_CASE: io.Message(tok+"[CaseStatement]"); tok.scan(); break; case Tok.TOK_1_SEMI: io.Message(tok+"[;]"); needBranch = false; tok.scan(); break; } do { stmt(procvar.nodes, label2, label1); } while (tok.getId() != Tok.TOK_END); if (needBranch) emit.Branch("br", label1); if (tok.getId() != Tok.TOK_END) io.Abort("PL0124: END expected"); io.Message(tok+"[END]"); tok.scan(); curtree.add(procvar); if (needBranch) emit.Label(label2); }
public void add(Var p) { nodes.add(p); }
void ident_stmt(VarList curtree) { Tok s = new Tok(null); VarList vars = new VarList(); Var V; int gotType = Tok.TOK_BINARY; s.setValue(tok.getValue()); s.setId(tok.getId()); tok.scan(); switch (tok.getId()) { case Tok.TOK_1_EQUALS: if (curtree.FindByName(s.getValue()) == null) io.Abort("PL0137: undeclared variable"); V = new Var(); V.setName(s.getValue()); vars.add(V); io.Message(s+"[Variable]"); io.Message(tok+"[Equals]"); tok.scan(); gotType = bool_expr(0); typeCheckAssign(gotType, curtree.FindByName(vars.FindByIndex(0).getName()).getTypeId()); null_stmt(); break; case Tok.TOK_1_COMMA: io.Message(s+"[Variable]"); if (curtree.FindByName(s.getValue()) == null) io.Abort("PL0137: undeclared variable"); V = new Var(); V.setName(s.getValue()); vars.add(V); while (tok.getId() != Tok.TOK_1_EQUALS) { if (tok.getFirstChar() != ',') io.Abort("PL0138: ',' or '=' expected"); io.Message(tok+"[Comma]"); tok.scan(); if (tok.getId() != Tok.TOK_IDENT) io.Abort("PL0139: ident expected"); if (curtree.FindByName(tok.getValue()) == null) io.Abort("PL0137: undeclared variable"); V = new Var(); V.setName(tok.getValue()); vars.add(V); io.Message(tok+"[Variable]"); tok.scan(); } io.Message(tok+"[Assign]"); tok.scan(); gotType = bool_expr(0); typeCheckAssign(gotType, curtree.FindByName(vars.FindByIndex(0).getName()).getTypeId()); null_stmt(); break; case Tok.TOK_1_REL: label(s); break; default: io.Abort("PL0140: not found expected token ':', ',' or '='"); break; } for (int i = 0; i < vars.Length(); i ++) { typeCheckAssign(gotType, curtree.FindByName(vars.FindByIndex(i).getName()).getTypeId()); emit.Store(curtree.FindByName(vars.FindByIndex(i).getName())); if (i < vars.Length() -1) emit.Load(curtree.FindByName(vars.FindByIndex(0).getName())); } }
private string genFieldRef(Var e) { if (e == null) return null; StringBuilder sb = new StringBuilder(Io.MAXSTR); if (e.getSign() == Tok.T_UNSIGNED) sb.Append("unsigned "); sb.Append(ilSType(e.getTypeId())); sb.Append(" "); sb.Append("Class" + io.GetClassname()); sb.Append("."); sb.Append(e.getName()); return (sb.ToString()); }