public void ForStmt() { var s = @" for a := 2; a < 10; a := a + 1 begin write a end"; var expected = new List <BaseAST> { new ForStmtAST(new Branch( new BinaryExprAST(ASTType.LessThan, new VariableExprAST("a"), new IntExprAST(10)), new BlockExprAST(new List <BaseAST> { new WriteStmtAST(new VariableExprAST("a")) } )), new AssignStmtAST("a", new IntExprAST(2)), new AssignStmtAST("a", new BinaryExprAST( ASTType.Add, new VariableExprAST("a"), new IntExprAST(1) )) ) }; Assert.AreEqual(expected, LangParser.ParseAll(s)); }
public string fromString(string input) { input = new Stubs() + input; ICharStream stream = CharStreams.fromString(input); ITokenSource lexer = new LangLexer(stream); ITokenStream tokens = new CommonTokenStream(lexer); var parser = new LangParser(tokens); parser.BuildParseTree = true; var tree = parser.prog(); var visitor = new Translator.Translator(tokens); var prog = visitor.VisitProg(tree); var env = new Environment(); prog.Validate(env); return(prog.ToString()); }
public static void Main(string[] args) { if (args.Length == 1) { string fullpath; if ( Path.IsPathRooted(args[0]) ) fullpath = args[0]; else fullpath = Path.Combine(Environment.CurrentDirectory, args[0]); Console.Out.WriteLine("Processing file: {0}", fullpath); ICharStream input = new ANTLRFileStream(fullpath); LangLexer lex = new LangLexer(input); CommonTokenStream tokens = new CommonTokenStream(lex); LangParser parser = new LangParser(tokens); //LangParser.decl_return r = parser.decl(); LangParser.start_return r = parser.start(); Console.Out.WriteLine("tree: "+((ITree)r.Tree).ToStringTree()); CommonTree r0 = ((CommonTree)r.Tree); CommonTreeNodeStream nodes = new CommonTreeNodeStream(r0); nodes.TokenStream = tokens; LangDumpDecl walker = new LangDumpDecl(nodes); walker.decl(); } else Console.Error.WriteLine("Usage: TreeParser <input-file>"); }
public static void Main(string[] args) { if (args.Length == 1) { string fullpath; if (Path.IsPathRooted(args[0])) { fullpath = args[0]; } else { fullpath = Path.Combine(Environment.CurrentDirectory, args[0]); } Console.Out.WriteLine("Processing file: {0}", fullpath); ICharStream input = new ANTLRFileStream(fullpath); LangLexer lex = new LangLexer(input); CommonTokenStream tokens = new CommonTokenStream(lex); LangParser parser = new LangParser(tokens); //LangParser.decl_return r = parser.decl(); LangParser.start_return r = parser.start(); Console.Out.WriteLine("tree: " + ((ITree)r.Tree).ToStringTree()); CommonTree r0 = ((CommonTree)r.Tree); CommonTreeNodeStream nodes = new CommonTreeNodeStream(r0); nodes.TokenStream = tokens; LangDumpDecl walker = new LangDumpDecl(nodes); walker.decl(); } else { Console.Error.WriteLine("Usage: TreeParser <input-file>"); } }
public void OnlyIf() { var s = "if 2<3 then 2 end"; var expected = new List <BaseAST> { new IfStmtAST( new List <Branch> { new Branch( new BinaryExprAST( ASTType.LessThan, new IntExprAST(2), new IntExprAST(3) ), new List <BaseAST> { new FloatExprAST(2.3), new VariableExprAST("a") }.ToBlock() ) }, null ) }; Assert.AreEqual(expected, LangParser.ParseAll(s)); }
public void RepeatStmt() { var s = @" repeat /* Comment */ a := 3; // 233 // 456 4 until true"; var expected = new List <BaseAST> { new RepeatStmtAST(new Branch( new BoolExprAST(true), new BlockExprAST(new List <BaseAST> { new AssignStmtAST("a", new IntExprAST(3)), new IntExprAST(4) } )) ) }; Assert.AreEqual(expected, LangParser.ParseAll(s)); }
public void ReadStmt() { var s = "read a"; var expected = new List <BaseAST> { new ReadStmtAST("a", TypeKind.Int) }; Assert.AreEqual(expected, LangParser.ParseAll(s)); }
public void WriteStmt() { var s = "write a"; var expected = new List <BaseAST> { new WriteStmtAST(new VariableExprAST("a")) }; Assert.AreEqual(expected, LangParser.ParseAll(s)); }
public void BoolParsesTrue() { var buffer = new ParseBuffer(@"true", 0); var ret = LangParser.Boolean(buffer); Assert.IsTrue(ret.IsSuccessful); Assert.IsTrue(ret.Result != null); // TODO replace with not null assert method Assert.IsTrue(ret.Result.Value); }
public static (LangParser, ITokenStream) GetTree(string code) { ICharStream stream = CharStreams.fromString(code); ITokenSource lexer = new LangLexer(stream); ITokenStream tokens = new CommonTokenStream(lexer); var parser = new LangParser(tokens); parser.BuildParseTree = true; return(parser, tokens); }
static void Main(string[] args) { using (StreamReader fileStream = new StreamReader(@"C:\Users\superblaubeere27\Desktop\compilerTest\Compiler\Compiler\test.lang")) { AntlrInputStream inputStream = new AntlrInputStream(fileStream); LangLexer lexer = new LangLexer(inputStream); CommonTokenStream commonTokenStream = new CommonTokenStream(lexer); LangParser parser = new LangParser(commonTokenStream); new CompilerVisitor().VisitFile(parser.file()); } }
public void Assign() { var s = "a := 3"; var expected = new List <BaseAST> { new AssignStmtAST( "a", new IntExprAST(3) ) }; Assert.AreEqual(expected, LangParser.ParseAll(s)); }
public void Compare() { var s = "a >= 3"; var expected = new List <BaseAST> { new BinaryExprAST( ASTType.GreaterEqual, new VariableExprAST("a"), new IntExprAST(3) ) }; Assert.AreEqual(expected, LangParser.ParseAll(s)); }
public static void Main(string[] args) { try { LangLexer lexer = new LangLexer(new CharBuffer(Console.In)); LangParser parser = new LangParser(lexer); parser.block(); CommonAST a = (CommonAST) parser.getAST(); Console.Out.WriteLine(a.ToStringList()); LangWalker walker = new LangWalker(); walker.block(a); // walk tree Console.Out.WriteLine("done walking"); } catch(Exception e) { Console.Error.WriteLine("exception: "+e); } }
public void MultiLine() { var s = @" a; 12"; var expected = new List <BaseAST> { new VariableExprAST("a"), new IntExprAST(12) }; Assert.AreEqual(expected, LangParser.ParseAll(s)); }
public static void Main(string[] args) { try { LangLexer lexer = new LangLexer(new CharBuffer(Console.In)); LangParser parser = new LangParser(lexer); parser.block(); CommonAST a = (CommonAST)parser.getAST(); Console.Out.WriteLine(a.ToStringList()); LangWalker walker = new LangWalker(); walker.block(a); // walk tree Console.Out.WriteLine("done walking"); } catch (Exception e) { Console.Error.WriteLine("exception: " + e); } }
public void Nested_If() { var s = @" if true then if false then write 4 else write 5 end end"; var expected = new List <BaseAST> { new IfStmtAST( new List <Branch> { new Branch( new BoolExprAST(true), new List <BaseAST> { new IfStmtAST( new List <Branch> { new Branch( new BoolExprAST(false), new List <BaseAST> { new WriteStmtAST(new IntExprAST(4)) }.ToBlock() ) }, new ElseBranch( new List <BaseAST> { new WriteStmtAST(new IntExprAST(5)) }.ToBlock() ) ) }.ToBlock() ) }, null ) }; Assert.AreEqual(expected, LangParser.ParseAll(s)); }
public void SingleLine() { var s = "a2a*(133+2.3);12"; var expected = new List <BaseAST> { new BinaryExprAST( ASTType.Multiply, new VariableExprAST("a2a"), new BinaryExprAST( ASTType.Add, new IntExprAST(133), new FloatExprAST(2.3) ) ), new IntExprAST(12) }; Assert.AreEqual(expected, LangParser.ParseAll(s)); }
public SelectTable[] Run() { SelectTable[] table = null; var parser = new LangParser(_source, _flags); var ast = parser.Parse(); parser.Errors.ToList().ForEach(x => Errors.Add(x)); if (Errors.Count == 0) { //semantic check if ((_flags & ExecuteFlags.Semantic) == ExecuteFlags.Semantic) { var inter = new Interpreter.Execution.Interpreter(ast, _editorCursor, Authorization, _semanticHttpExecutor); inter.Execute(); if (inter.Errors.Count > 0) { Errors = inter.Errors; } ScopeModel = inter.ScopeModel; } if (Authorization == "Basic" && Errors.Count == 0) //empty auth header { Errors.Add(new ApiExecuteError(ApiExecuteErrorType.AuthError, "Authentication error")); return(table); } if (Errors.Count == 0 && ((_flags & ExecuteFlags.Execute) == ExecuteFlags.Execute)) // we run { var inter = new Interpreter.Execution.Interpreter(ast, _editorCursor, Authorization, _httpExecutor); table = inter.Execute(); Errors = inter.Errors; } } return(table); }
public void AndOr() { var s = "a >= 2 && true || false"; var expected = new List <BaseAST> { new BinaryExprAST( ASTType.Or, new BinaryExprAST( ASTType.And, new BinaryExprAST( ASTType.GreaterEqual, new VariableExprAST("a"), new IntExprAST(2) ), new BoolExprAST(true) ), new BoolExprAST(false) ) }; Assert.AreEqual(expected, LangParser.ParseAll(s)); }
private void CheckIt(string s) => _checker.Visit(LangParser.ParseAll(s));
public void LangParser_ExecuteCode_Expect_Results() { var lang = new LangParser(); lang.Execute("print \"hola mundo\""); }
static void Main(string[] args) { var code = @" func Get1() { return 1; } proc Test() { Print(5); } proc PrintAddMul(x, y, z) { Print(x + y * z); } proc TestRet1(x) { if (x) { return; } else { // return; // unreachable code } return; } func TestRet2(x) { if (x) { return 1; } else { // return; // unreachable code } return 3; // must have } func TestRet3(x) { if (x) { return x; } else { return -x; } } func PrintAndGet(p, r) { Print(p); return r; } proc TestAndOr() { PrintAndGet(30, 0) && PrintAndGet(30, 1); // 30 PrintAndGet(31, 1) && PrintAndGet(31, 1); // 31 31 PrintAndGet(32, 1) || PrintAndGet(32, 1); // 32 PrintAndGet(33, 0) || PrintAndGet(33, 1); // 33 33 } proc Main(a, b, c) { var mvar; mvar = 11; Test(); Print(bbb + a); a = 5; Get1(); PrintAddMul(a + b, Get1(), mvar); EndSection(); Print(1 < 2); Print(1 < 0); Print(1 == 2); Print(1 == 1); if (2) { Print(333); } else { Print(222); } if (0) { Print(18); } else { Print(81); } EndSection(); TestAndOr(); EndSection(); setMe = 999; TestRet1(1); Print(TestRet2(2)); Print(TestRet3(3)); EndSection(); } "; var compiler = new Compiler(typeof(ExecutionContext)); { var parser = new LangParser(new CommonTokenStream(new LangLexer(new AntlrInputStream(code)))); parser.RemoveErrorListeners(); parser.AddErrorListener(new ThrowingErrorListener()); var tree = TreeTransformer.Transform(parser.program()); var result = compiler.Compile(tree); result.GetMethod("Main").Invoke(null, new object[] { 1, 2, 3 }); } Console.WriteLine("@@@ WITH MY PARSER @@@"); { var stream = new MemoryStream(); var writer = new StreamWriter(stream); writer.Write(code); writer.Flush(); stream.Position = 0; var parser = new LanguageParser.SimpleParser(); var tree = parser.Parse(new LanguageParser.SimpleLexer(stream)); var result = compiler.Compile(tree); result.GetMethod("Main").Invoke(null, new object[] { 1, 2, 3 }); } }
public void LangParser_ExecuteCodeWithExpression_Expect_Results() { var lang = new LangParser(); lang.Execute("print {1}"); }
public void AtLeastOneStmt() { var s = @""; Assert.Throws <ParseException>(() => LangParser.ParseAll(s)); }