private ISTMT ParseStmt() { try { if (i + 1 < tokens.Length && tokens[i + 1].val.ToString() == "(") { if (isfunction) { string name = Current.val.ToString(); i++; i++; FuncCall f = new FuncCall() { method = name, args = ParseArgs() }; i++; return f; } else { string name = Current.val.ToString(); i++; i++; DefineFunc f = new DefineFunc() { name = name, Args = ParseFuncArgs() }; isfunction = true; i++; f.body = ParseBody(); isfunction = false; if (isClass) { cfs.Add(f); } return f; } } else if (i < tokens.Length && Current.type == TokenType.Base) { BaseCode bc = new BaseCode() { val = Current.val.ToString() }; i++; return bc; } else if (i < tokens.Length && Current.val.ToString() == "open") { i++; Open o = new Open() { name = Current.val.ToString() }; i++; return o; } else if (i + 1 < tokens.Length && Current.val.ToString() == "xmlgen") { i++; string XML = Current.val.ToString(); Xml x = new Xml() { text = XML }; i++; return x; } else if (i + 1 < tokens.Length && tokens[i + 1].val.ToString() == "=") { string name = Current.val.ToString(); i++; i++; SetVar s = new SetVar() { name = name, value = ParseExpr(false) }; i++; return s; } else if (i + 1 < tokens.Length && Current.val.ToString() == "if") { If f = new If(); i++; f.eval = ParseExpr(false); i++; f.truebody = ParseBody(); i--; if (Current.val.ToString() == "else") { i++; f.falsebody = ParseBody(); } else { i++; } return f; } else if (i < tokens.Length && Current.val.ToString() == "from") { i++; string inc = Current.val.ToString(); i++; //name i++; //, IEXPR to = ParseExpr(false); i++; //expr i++; //, IEXPR by = ParseExpr(false); i++; //by ISTMT[] body = ParseBody(); Loop l = new Loop() { from = inc, to = to, by = by, body = body }; return l; } else if (i < tokens.Length && Current.val.ToString() == "while") { i++; IEXPR expr = ParseExpr(false); i++; ISTMT[] body = ParseBody(); While w = new While() { expr = expr, body = body }; return w; } else if (i < tokens.Length && Current.val.ToString() == "return") { i++; IEXPR v = ParseExpr(false); i++; Return r = new Return() { value = v }; return r; } else if (i < tokens.Length && Current.val.ToString() == "class") { i++; string name = Current.val.ToString(); i++; DefClass d = new DefClass(); d.Name = name; isClass = true; cref = d; cfs = new List<DefineFunc>(); cvs = new List<DefVar>(); ParseBody(); d.funcs = cfs.ToArray(); d.vars = cvs.ToArray(); isClass = false; return d; } else if (i < tokens.Length && Current.val.ToString() == "public") { i++; string name = Current.val.ToString(); i++; DefVar v = new DefVar() { access = "public", name = name }; if (isClass) { cvs.Add(v); } return v; } else if (i < tokens.Length && Current.val.ToString() == "private") { i++; string name = Current.val.ToString(); i++; DefVar v = new DefVar() { access = "private", name = name }; if (isClass) { cvs.Add(v); } return v; } else if (i < tokens.Length && Current.val.ToString() == "\n") { i++; return null; } else if (i < tokens.Length && Current.val.ToString() == "end") { i++; return new End(); } else if (i < tokens.Length && Current.val.ToString() == "else") { i++; return new Else(); } else { throw new Exception("Unkown token"); } } catch (IndexOutOfRangeException) { MessageBox.Show("Unexpected end of file","Error"); return null; } }
private ISTMT ParseStmt() { try { if (i + 1 < tokens.Length && tokens[i + 1].val.ToString() == "(") { if (isfunction) { string name = Current.val.ToString(); i++; i++; FuncCall f = new FuncCall() { method = name, args = ParseArgs() }; i++; return(f); } else { string name = Current.val.ToString(); i++; i++; DefineFunc f = new DefineFunc() { name = name, Args = ParseFuncArgs() }; isfunction = true; i++; f.body = ParseBody(); isfunction = false; if (isClass) { cfs.Add(f); } return(f); } } else if (i < tokens.Length && Current.type == TokenType.Base) { BaseCode bc = new BaseCode() { val = Current.val.ToString() }; i++; return(bc); } else if (i < tokens.Length && Current.val.ToString() == "open") { i++; Open o = new Open() { name = Current.val.ToString() }; i++; return(o); } else if (i + 1 < tokens.Length && Current.val.ToString() == "xmlgen") { i++; string XML = Current.val.ToString(); Xml x = new Xml() { text = XML }; i++; return(x); } else if (i + 1 < tokens.Length && tokens[i + 1].val.ToString() == "=") { string name = Current.val.ToString(); i++; i++; SetVar s = new SetVar() { name = name, value = ParseExpr(false) }; i++; return(s); } else if (i + 1 < tokens.Length && Current.val.ToString() == "if") { If f = new If(); i++; f.eval = ParseExpr(false); i++; f.truebody = ParseBody(); i--; if (Current.val.ToString() == "else") { i++; f.falsebody = ParseBody(); } else { i++; } return(f); } else if (i < tokens.Length && Current.val.ToString() == "from") { i++; string inc = Current.val.ToString(); i++; //name i++; //, IEXPR to = ParseExpr(false); i++; //expr i++; //, IEXPR by = ParseExpr(false); i++; //by ISTMT[] body = ParseBody(); Loop l = new Loop() { from = inc, to = to, by = by, body = body }; return(l); } else if (i < tokens.Length && Current.val.ToString() == "while") { i++; IEXPR expr = ParseExpr(false); i++; ISTMT[] body = ParseBody(); While w = new While() { expr = expr, body = body }; return(w); } else if (i < tokens.Length && Current.val.ToString() == "return") { i++; IEXPR v = ParseExpr(false); i++; Return r = new Return() { value = v }; return(r); } else if (i < tokens.Length && Current.val.ToString() == "class") { i++; string name = Current.val.ToString(); i++; DefClass d = new DefClass(); d.Name = name; isClass = true; cref = d; cfs = new List <DefineFunc>(); cvs = new List <DefVar>(); ParseBody(); d.funcs = cfs.ToArray(); d.vars = cvs.ToArray(); isClass = false; return(d); } else if (i < tokens.Length && Current.val.ToString() == "public") { i++; string name = Current.val.ToString(); i++; DefVar v = new DefVar() { access = "public", name = name }; if (isClass) { cvs.Add(v); } return(v); } else if (i < tokens.Length && Current.val.ToString() == "private") { i++; string name = Current.val.ToString(); i++; DefVar v = new DefVar() { access = "private", name = name }; if (isClass) { cvs.Add(v); } return(v); } else if (i < tokens.Length && Current.val.ToString() == "\n") { i++; return(null); } else if (i < tokens.Length && Current.val.ToString() == "end") { i++; return(new End()); } else if (i < tokens.Length && Current.val.ToString() == "else") { i++; return(new Else()); } else { throw new Exception("Unkown token"); } } catch (IndexOutOfRangeException) { MessageBox.Show("Unexpected end of file", "Error"); return(null); } }
private IEXPR ParseExpr(bool isArith) { IEXPR r = null; if (Current.type == TokenType.String_Literal) { string val = Current.val.ToString(); StringLit s = new StringLit() { val = val }; r = s; } else if (Current.type == TokenType.Int_Literal) { int val = (int)Current.val; IntLit i = new IntLit() { val = val }; r = i; } else if (Current.type == TokenType.Double_Literal) { double val = (double)Current.val; DoubleLit d = new DoubleLit() { val = val }; r = d; } else if (Current.type == TokenType.Word && tokens[this.i + 1].val.ToString() == "(") { string name = Current.val.ToString(); i++; i++; FuncCall f = new FuncCall() { method = name, args = ParseArgs() }; r = f; } else if (Current.val.ToString() == "new") { i++; New n = new New() { from = Current.val.ToString() }; r = n; } else if (Current.type == TokenType.Bool_Literal) { BoolLit b = new BoolLit() { val = (bool)Current.val }; r = b; } else if (Current.type == TokenType.Word) { string name = Current.val as string; LoadVar v = new LoadVar() { name = name }; r = v; } if ((this.i + 1 < tokens.Length) && (isArith == false) && (tokens[this.i + 1].val.ToString() == "+" || tokens[this.i + 1].val.ToString() == "-" || tokens[this.i + 1].val.ToString() == "*" || tokens[this.i + 1].val.ToString() == "/" || tokens[this.i + 1].val.ToString() == "%" || tokens[this.i + 1].val.ToString() == "<" || tokens[this.i + 1].val.ToString() == "=" || tokens[this.i + 1].val.ToString() == ">" || Current.val.ToString() == "(")) { r = ParseAtrithExpr(r); i++; return r; } else { return r; } throw new Exception(); }
private IEXPR ParseExpr(bool isArith) { IEXPR r = null; if (Current.type == TokenType.String_Literal) { string val = Current.val.ToString(); StringLit s = new StringLit() { val = val }; r = s; } else if (Current.type == TokenType.Int_Literal) { int val = (int)Current.val; IntLit i = new IntLit() { val = val }; r = i; } else if (Current.type == TokenType.Double_Literal) { double val = (double)Current.val; DoubleLit d = new DoubleLit() { val = val }; r = d; } else if (Current.type == TokenType.Word && tokens[this.i + 1].val.ToString() == "(") { string name = Current.val.ToString(); i++; i++; FuncCall f = new FuncCall() { method = name, args = ParseArgs() }; r = f; } else if (Current.val.ToString() == "new") { i++; New n = new New() { from = Current.val.ToString() }; r = n; } else if (Current.type == TokenType.Bool_Literal) { BoolLit b = new BoolLit() { val = (bool)Current.val }; r = b; } else if (Current.type == TokenType.Word) { string name = Current.val as string; LoadVar v = new LoadVar() { name = name }; r = v; } if ((this.i + 1 < tokens.Length) && (isArith == false) && (tokens[this.i + 1].val.ToString() == "+" || tokens[this.i + 1].val.ToString() == "-" || tokens[this.i + 1].val.ToString() == "*" || tokens[this.i + 1].val.ToString() == "/" || tokens[this.i + 1].val.ToString() == "%" || tokens[this.i + 1].val.ToString() == "<" || tokens[this.i + 1].val.ToString() == "=" || tokens[this.i + 1].val.ToString() == ">" || Current.val.ToString() == "(")) { r = ParseAtrithExpr(r); i++; return(r); } else { return(r); } throw new Exception(); }