public override void Init(Irony.Parsing.ParsingContext context, Irony.Parsing.ParseTreeNode treeNode) { base.Init(context, treeNode); AddChild("Block", treeNode.ChildNodes[3]); localScope = new Scope(); var parameters = treeNode.ChildNodes[2].ChildNodes; for (int i = 0; i < parameters.Count; ++i) { var variable = new Variable(); variable.scope = localScope; variable.name = parameters[i].ChildNodes[0].FindTokenAndGetText(); localScope.variables.Add(variable); if (i < 3) { variable.location = (Register)i; localScope.UseRegister(i); } else { variable.location = Register.STACK; variable.stackOffset = localScope.stackDepth; localScope.stackDepth += 1; } parameterCount += 1; } this.AsString = treeNode.ChildNodes[1].FindTokenAndGetText(); label = Scope.GetLabel() + "_" + AsString; localScope.activeFunction = this; }
public static void CompileRoot(FunctionDeclarationNode root, Assembly assembly, Action<string> onError) { DCPUC.Scope.Reset(); var scope = new DCPUC.Scope(); var end_of_program = new Variable(); end_of_program.location = Register.STATIC; end_of_program.name = "__endofprogram"; end_of_program.staticLabel = "ENDOFPROGRAM"; end_of_program.emitBrackets = false; scope.variables.Add(end_of_program); var library = new List<String>(System.IO.File.ReadAllLines("libdcpuc.txt")); //root.InsertLibrary(library); try { root.CompileFunction(assembly, scope); foreach (var dataItem in DCPUC.Scope.dataElements) { var datString = ""; foreach (var item in dataItem.Item2) { datString += item; datString += ", "; } assembly.Add(":" + dataItem.Item1, "DAT", datString.Substring(0, datString.Length - 2)); } assembly.Add(":ENDOFPROGRAM", "", ""); } catch (DCPUC.CompileError c) { onError(c.Message); return; } }
public override void Compile(List<string> assembly, Scope scope) { var newVariable = new Variable(); newVariable.name = AsString; newVariable.scope = scope; newVariable.stackOffset = scope.stackDepth; (ChildNodes[0] as CompilableNode).Compile(assembly, scope); scope.variables.Add(newVariable); scope.stackDepth += 1; }
public override void Init(Irony.Parsing.ParsingContext context, Irony.Parsing.ParseTreeNode treeNode) { base.Init(context, treeNode); AddChild("Block", treeNode.ChildNodes[3]); foreach (var parameter in treeNode.ChildNodes[2].ChildNodes) { var variable = new Variable(); variable.scope = localScope; variable.name = parameter.ChildNodes[0].FindTokenAndGetText(); variable.stackOffset = localScope.stackDepth; localScope.variables.Add(variable); localScope.stackDepth += 1; parameterCount += 1; } this.AsString = treeNode.ChildNodes[1].FindTokenAndGetText(); label = Scope.GetLabel() + "_" + AsString; localScope.activeFunction = this; }
public override void Compile(Assembly assembly, Scope scope, Register target) { var newVariable = new Variable(); newVariable.name = AsString; newVariable.scope = scope; newVariable.stackOffset = scope.stackDepth; if (declLabel == "var") { newVariable.location = (Register)scope.FindAndUseFreeRegister(); if (newVariable.location == Register.I) newVariable.location = Register.STACK; if (ChildNodes[0] is BlockLiteralNode) { var size = Child(0).GetConstantValue(); scope.stackDepth += size; assembly.Add("SUB", "SP", hex(size)); assembly.Add("SET", Scope.GetRegisterLabelFirst((int)newVariable.location), "SP"); if (newVariable.location == Register.STACK) scope.stackDepth += 1; } else (ChildNodes[0] as CompilableNode).Compile(assembly, scope, newVariable.location); } else if (declLabel == "static") { newVariable.location = Register.STATIC; if (Child(0) is BlockLiteralNode) { newVariable.staticLabel = Scope.GetLabel() + "_STATIC_" + AsString; var datLabel = Scope.GetLabel() + "_STATIC_" + AsString + "_DATA"; Scope.AddData(datLabel, (ChildNodes[0] as BlockLiteralNode).MakeData()); Scope.AddData(newVariable.staticLabel, datLabel); } else if ((ChildNodes[0] as CompilableNode).IsConstant()) { newVariable.staticLabel = Scope.GetLabel() + "_STATIC_" + AsString; Scope.AddData(newVariable.staticLabel, new List<ushort>(new ushort[] { (ChildNodes[0] as CompilableNode).GetConstantValue() })); } else if (ChildNodes[0] is DataLiteralNode) { newVariable.staticLabel = Scope.GetLabel() + "_STATIC_" + AsString; var datLabel = Scope.GetLabel() + "_STATIC_" + AsString + "_DATA"; Scope.AddData(datLabel, (ChildNodes[0] as DataLiteralNode).data); Scope.AddData(newVariable.staticLabel, datLabel); } else throw new CompileError("Statics must be initialized to a constant value"); } else if (declLabel == "const") { newVariable.location = Register.CONST; if (Child(0) is BlockLiteralNode) { newVariable.staticLabel = Scope.GetLabel() + "_STATIC_" + AsString; Scope.AddData(newVariable.staticLabel, (ChildNodes[0] as BlockLiteralNode).MakeData()); } else if ((ChildNodes[0] as CompilableNode).IsConstant()) { //throw new CompileError("Initializing const to integrals is not supported yet"); newVariable.staticLabel = (ChildNodes[0] as CompilableNode).GetConstantToken(); //Scope.AddData(newVariable.staticLabel, (ChildNodes[0] as CompilableNode).GetConstantToken()); } else if (ChildNodes[0] is DataLiteralNode) { newVariable.staticLabel = Scope.GetLabel() + "_STATIC_" + AsString; Scope.AddData(newVariable.staticLabel, (ChildNodes[0] as DataLiteralNode).data); } } scope.variables.Add(newVariable); //scope.stackDepth += 1; }