public void TestBase() { var context0 = Substitute.For<ILineContext>(); context0.ToXML().Returns(new XElement("LineContext", new XAttribute[] { new XAttribute("pos", 0), new XAttribute("line", 0) })); var context1 = Substitute.For<ILineContext>(); context1.ToXML().Returns(new XElement("LineContext", new XAttribute[] { new XAttribute("pos", 1), new XAttribute("line", 2)})); var context2 = Substitute.For<ILineContext>(); context2.ToXML().Returns(new XElement("LineContext", new XAttribute[] { new XAttribute("pos", 3), new XAttribute("line", 4) })); Exp exp1 = Substitute.For<Exp>(); Exp exp2 = Substitute.For<Exp>(); Base node = new Base(context0); FunctionCallExp func = new FunctionCallExp(context0); func.Name = new Identifier("funcName"); ParameterCall param1 = new ParameterCall(context1, exp1); param1.Name = "param1"; ParameterCall param2 = new ParameterCall(context2, exp2); param2.Name = "param2"; func.Args.Add(param1); func.Args.Add(param2); node.Children.Add(func); Assert.AreEqual( @"<Base> <FunctionCallExp Name=""funcName""> <ParameterCall Name=""param1"" /> <ParameterCall Name=""param2"" /> </FunctionCallExp> </Base>", node.ToXML(new XMLParser.XMLProperties(false, false)).ToString()); }
public Base CheckSyntax(List<Token> tokens, List<ILineContext> context, Global.InstructionSets architecture) { allTokens = tokens; allContext = context; this.architecture = architecture; astBase = new Base(context[0]); EatStatements(); return astBase; }
public void TestCheckVarDeclaration() { Base ast = new Base(new LineContext(1, 1)); /*ASTNode astVariable = new ASTNode(Global.ASTType.VAR_DECLARATION, new ILineContext(1, 2)); astVariable.SetName("a"); ast.SetChildren(new List<ASTNode> { astVariable }); SemanticAnalyzer semanticAnalyzer = new SemanticAnalyzer(); List<Table> tables = semanticAnalyzer.GenerateSymbolTables(ast); Assert.AreEqual("a", tables[1].lookup("a").GetName());*/ }
public void TestGenerateSymbolTables() { Base ast = new Base(new LineContext(1, 1)); /*ASTNode astFunction = new ASTNode(Global.ASTType.FUNCTION_CALL, new ILineContext(1, 2)); astFunction.SetName("print"); ASTNode astString = new ASTNode(Global.ASTType.STRING, new ILineContext(2, 2)); astString.SetName("testje"); astFunction.SetChildren(new List<ASTNode> { astString }); ast.SetChildren(new List<ASTNode> { astFunction }); SemanticAnalyzer semanticAnalyzer = new SemanticAnalyzer(); List<Table> tables = semanticAnalyzer.GenerateSymbolTables(ast); Assert.AreEqual("print", tables[0].lookup("print").GetName());*/ }
public List<Module> GenerateCode(string source, string dest, Base ast, List<Table> tables) { //tables[1].Compress(); // Doesn't work in a situation like a=5; b=a; print(b) this.tables = tables; //result = new List<string>(); //w("file:\"" + source + "\""); //w("section:constants"); //teller = 0; //SearchConstants(ast); //w("section:var_unitialised"); //teller = 0; //SearchUnitialised(ast); //w("section:var_initialised"); //teller = 0; //SearchInitialised(ast); Add(new SectionCode()); Add(new MakeGlobal("main")); Add(new Label("main")); Add(new Push(new Register(Global.Registers.STACKBASEPOINTER))); Add(new Move(new Register(Global.Registers.STACKPOINTER), regBase)); Add(new Sub(new ByteConstant(4), regStack)); if (tables[1].GetStackSize() > 0) Add(new Sub(new ByteConstant(tables[1].GetStackSize()), regStack)); Add(new Call("__main")); /*w("section:code"); w("define_main_method"); w("set_base_pointer"); w("reserve_stack"); w("call:%SETUP_C%");*/ stack.Clear(); ast.accept(this); Add(new Nope()); Add(new Move(new Register(Global.Registers.STACKBASEPOINTER), new Register(Global.Registers.STACKPOINTER))); Add(new Pop(new Register(Global.Registers.STACKBASEPOINTER))); Add(new Ret()); /*w("get_base_pointer"); w("return"); w("comment:\"Yontu: (Joost Verbraeken) BETA\"");*/ return new List<Module>() { new Module(postfixList, stringTable) }; }
public void TestPrintOneString() { ILineContext context = Substitute.For<ILineContext>(); Base ast = new Base(null); ast.Children.Add(new FunctionCallExp(null)); ((FunctionCallExp)ast.Children[0]).Name = new Identifier("print"); //((FunctionCallExp)ast.Children[0]).Args.Add(new ParameterCall(context, new StringLiteral(null, "Hello World!"))); Table builtinTable = SemanticAnalyzer.CreateBuiltinSymbols(); List<Module> modules = generator.GenerateCode("", "", ast, new List<Table>() { builtinTable, new Table(builtinTable, Global.Scope.BuiltinScope, "builtin") }); List<Instruction> instructions = modules[0].InterCode; Assert.IsTrue(instructions[7] is StringAsParameter); Assert.AreEqual(".LC0", ((StringAsParameter)instructions[7]).Name); Assert.AreEqual(0, ((StringAsParameter)instructions[7]).Number); Assert.IsTrue(instructions[8] is Call); Assert.AreEqual("printf", ((Call)instructions[8]).Name); }
public List<Table> CheckSemantics(Base ast) { tables = new List<Table>(); Table swiftTable = CreateBuiltinSymbols(); tables.Add(swiftTable); tables.Add(new Table(tables[0], Global.Scope.MainScope, "Main")); ScopeAssignVisitor scopeAssignVisitor = new ScopeAssignVisitor(); scopeAssignVisitor.Scope = tables[1]; // First we assign a scope to all nodes foreach (ASTNode node in ast.Children) { node.accept(scopeAssignVisitor); } // Then we generate the symbol tables and check the semantics foreach (ASTNode node in ast.Children) node.accept(this); return tables; }
public void TestPrintFiveIntegers() { ILineContext context = Substitute.For<ILineContext>(); Base ast = new Base(null); ast.Children.Add(new FunctionCallExp(null)); ((FunctionCallExp)ast.Children[0]).Name = new Identifier("print"); ((FunctionCallExp)ast.Children[0]).Args.Add(new ParameterCall(context, new Int64Literal(null, "5"))); Table builtinTable = SemanticAnalyzer.CreateBuiltinSymbols(); List<Module> modules = generator.GenerateCode("", "", ast, new List<Table>() { builtinTable, new Table(builtinTable, Global.Scope.BuiltinScope, "builtin") }); List<Instruction> instructions = modules[0].InterCode; Assert.IsTrue(instructions[7] is Move); Assert.IsTrue(((Move)instructions[7]).To is ParamRegister); Assert.AreEqual(1, ((ParamRegister)((Move)instructions[7]).To).Position); Assert.IsTrue(instructions[8] is Lea); Assert.IsTrue(((Lea)instructions[8]).From is RegisterOffset); Assert.AreEqual(".LC0", ((RegisterOffset)((Lea)instructions[8]).From).LabelOffset); Assert.AreEqual(Global.Registers.INSTRUCTIONPOINTER, ((RegisterOffset)((Lea)instructions[8]).From).Register); Assert.IsTrue(((Lea)instructions[8]).To is ParamRegister); Assert.AreEqual(0, ((ParamRegister)((Lea)instructions[8]).To).Position); Assert.IsTrue(instructions[9] is Call); Assert.AreEqual("printf", ((Call)instructions[9]).Name); //ff expres fout gedaan om de test te laten mislukken }
public override void visit(Base n) { foreach (ASTNode node in n.Children) node.accept(this); }
public abstract void visit(Base n);
public void visit(Base n) { throw new NotImplementedException(); }
public virtual void visit(Base n) { // Do nothing; leave the implementation to the main class }
public override void visit(Base n) { //The base IS the global scope }
public ASTType visit(Base n) { throw new NotImplementedException(); }