public override List<InstructionModel> GetInstructions(CompilationModel model) { var instructions = new List<InstructionModel>(); var classModel = GetCorrectParent(model); // push args foreach (var param in Parameters) { instructions.AddRange(param.GetInstructions(model)); } instructions.Add(new InstructionModel(Instructions.LoadPointerInstruction, "0") { Comment = model.GetComment(this) + " - loading self" }); try { instructions.Add(new InstructionModel(Instructions.CallInstruction, string.Format("{0}::{1}::{2}", classModel.Name != FindParent<Class>().Name ? classModel.Name : string.Empty, Method, Parameters.Count())) { Comment = model.GetComment(this) + " - doing method call" }); } catch (Exception e) { if (Method != "New" && Parameters.Count() != 0) { throw e; } } return instructions; }
public override void Compile(CompilationModel model) { foreach (var module in Modules) { module.Compile(model); } }
public override List<InstructionModel> GetInstructions(CompilationModel model) { var instructions = new List<InstructionModel>(); instructions.AddRange(Expression.GetInstructions(model)); instructions.Add(new InstructionModel(Instructions.LogicalNegIntInstruction) { Comment = model.GetComment(this) }); return instructions; }
public override List<InstructionModel> GetInstructions(CompilationModel model) { var instructions = new List<InstructionModel>(); instructions.AddRange(From.GetInstructions(model)); instructions.AddRange(To.GetInstructions(model)); return instructions; }
public override void Compile(CompilationModel model) { foreach (var @class in Classes) { @class.Compile(model); } }
public override List<InstructionModel> GetInstructions(CompilationModel model) { var instructions = new List<InstructionModel>(); instructions.Add(new InstructionModel(Instruction.Value.Replace("\"", ""))); // it ignore instrunction model, it emits what what user types instructions.Latest().Comment = model.GetComment(this); return instructions; }
private static void Main(string[] args) { Console.WriteLine("Language Scrappy Compiler 1.0"); if (args.Length == 0) { Console.WriteLine("usage: Scrappy <file.sp>"); return; } var grammar = CompiledGrammar.Load(typeof(Program), "Scrappy.egt"); var actions = new SemanticTypeActions<BaseToken>(grammar); try { actions.Initialize(true); } catch (InvalidOperationException ex) { Console.Write("Error: " + ex.Message); return; } try { var path = args[0]; var outputName = Path.GetFileNameWithoutExtension(path) + ".xml"; using (var reader = File.OpenText(path)) { var processor = new SemanticProcessor<BaseToken>(reader, actions); ParseMessage parseMessage = processor.ParseAll(); if (parseMessage == ParseMessage.Accept) { Console.WriteLine("Parsing done."); var compilationModel = new CompilationModel(File.ReadAllLines(path)); var start = (Start)processor.CurrentToken; start.Compile(compilationModel); // first classes, fields and methods needs to be compiled compilationModel.Compile(); // after that compile method body Console.WriteLine("Compiling done."); using (var outfile = new StreamWriter(outputName)) { outfile.Write(compilationModel.ToXml()); } // PrintAst(start); // only for debugging } else { IToken token = processor.CurrentToken; Console.WriteLine(token.Symbol); Console.WriteLine("Error: Line: {0} Column: {1} Error: {2}", token.Position.Line, token.Position.Column, parseMessage); } } } catch (Exception e) { Console.WriteLine("Error" + e.Message); } }
public override List<InstructionModel> GetInstructions(CompilationModel model) { var instructions = new List<InstructionModel>(); var index = model.GetClass(Expression.GetExpressionType(model)).GetFieldIndex(Property).ToString(CultureInfo.InvariantCulture); instructions.AddRange(Expression.GetInstructions(model)); instructions.Add(new InstructionModel(Instructions.SetFieldInstruction, index) { Comment = model.GetComment(this) + " - setting field on expression" }); return instructions; }
public override List<InstructionModel> GetInstructions(CompilationModel model) { var instructions = new List<InstructionModel>(); // casting is not important for compiled version instructions.AddRange(Expression.GetInstructions(model)); instructions.Latest().Comment = model.GetComment(this); return instructions; }
public ClassModel(string name, string parentName, CompilationModel model, bool skip = false) { Name = name; ParentName = parentName; CompilationModel = model; Skip = skip; Fields = new List<FieldModel>(); Methods = new List<MethodModel>(); }
public override List<InstructionModel> GetInstructions(CompilationModel model) { var instructions = new List<InstructionModel>(); instructions.AddRange(LeftExpression.GetInstructions(model)); instructions.AddRange(RightExpression.GetInstructions(model)); instructions.Add(new InstructionModel(Instructions.ModIntInstruction)); instructions.Latest().Comment = model.GetComment(this); return instructions; }
public override List<InstructionModel> GetInstructions(CompilationModel model) { var instructions = new List<InstructionModel> { new InstructionModel(Instructions.NewStringInstruction, "#" + Value.Replace("\"", "")) }; instructions.Latest().Comment = model.GetComment(this); return instructions; }
public override List<InstructionModel> GetInstructions(CompilationModel model) { var instructions = new List<InstructionModel>(); foreach (var statement in Statements) { instructions.AddRange(statement.GetInstructions(model)); } return instructions; }
public override List<InstructionModel> GetInstructions(CompilationModel model) { var instructions = new List<InstructionModel> { new InstructionModel(Instructions.PushIntInstruction, Value.ToString(CultureInfo.InvariantCulture)) }; instructions.Latest().Comment = model.GetComment(this); return instructions; }
public override List<InstructionModel> GetInstructions(CompilationModel model) { var instructions = new List<InstructionModel>(); instructions.AddRange(LeftExpression.GetInstructions(model)); instructions.AddRange(RightExpression.GetInstructions(model)); instructions.Add(new InstructionModel(Instructions.MulIntInstruction)); return instructions; }
public override string GetExpressionType(CompilationModel model) { var fullName = string.Format("{0}:{1}", Method, String.Join(":", Parameters.Select(e => e.GetExpressionType(model)))); if (Expression != null) { return model.GetClass(Expression.GetExpressionType(model)).GetMethod(fullName).Type; } return model.GetClass(FindParent<Class>().Name).GetMethod(fullName).Type; }
public override List<InstructionModel> GetInstructions(CompilationModel model) { var method = FindParent<Method>(); var @class = (Class)method.Parent; var classModel = model.GetClass(@class.Name); var index = classModel.GetFieldIndex(Property).ToString(CultureInfo.InvariantCulture); var instructions = new List<InstructionModel>(); instructions.Add(new InstructionModel(Instructions.LoadPointerInstruction, "0") { Comment = model.GetComment(this) + " - setting field" }); instructions.Add(new InstructionModel(Instructions.SetFieldInstruction, index) { Comment = model.GetComment(this) + " - setting field" }); return instructions; }
public override void Compile(CompilationModel model) { var methodModel = new MethodModel(Name, Type.Name, Block); // block will be compiled later model.Classes.Latest().Methods.Add(methodModel); methodModel.Locals.AddRange(Block.GetLocals()); foreach (var arg in Arguments) { methodModel.Arguments.Add(new ArgumentModel(arg.Name, arg.Type.Name)); } methodModel.Locals.Add(new LocalModel(LocalModel.TempVariable, BuiltinTypes.Any)); }
public override List<InstructionModel> GetInstructions(CompilationModel model) { var blockInstructions = Block.GetInstructions(model); var jmpTo = (blockInstructions.Count + 1).ToString(CultureInfo.InvariantCulture); var instructions = new List<InstructionModel>(); instructions.Add(new InstructionModel(Instructions.PushIntInstruction, "1")); instructions.Latest().Comment = model.GetComment(this); instructions.AddRange(Expression.GetInstructions(model)); instructions.Add(new InstructionModel(Instructions.IfIntEqInstruction, jmpTo)); instructions.AddRange(blockInstructions); return instructions; }
public void Compile(CompilationModel model) { Instructions.AddRange(Block.GetInstructions(model)); // add return instruction on end if it isnt already there if (Instructions.Count == 0 || (Instructions.Latest().Value != Compiler.Instructions.ReturnInstruction && Instructions.Latest().Value != Compiler.Instructions.ReturnIntInstruction && Instructions.Latest().Value != Compiler.Instructions.ReturnPointerInstruction)) { Instructions.Add(new InstructionModel(Compiler.Instructions.ReturnInstruction)); Instructions.Latest().Comment = "Added by compiler"; } }
public override string GetExpressionType(CompilationModel model) { var method = FindParent<Method>(); var @class = (Class)method.Parent; var methodModel = model.GetClass(@class.Name).GetMethod(method.FullName); try { return methodModel.GetVariableType(Variable); } catch (Exception) { return Variable; // in this case it's static call } }
public void Compile(CompilationModel model) { // fields are already precompiled // add fields from parent and keep order var oldFields = Fields; Fields = new List<FieldModel>(ParentClassModel.Fields); Fields.AddRange(oldFields); foreach (var methodModel in Methods) { methodModel.Compile(model); } }
public override void Compile(CompilationModel model) { model.Classes.Add(new ClassModel(Name, ParentName, model)); foreach (var property in Properties) { property.Compile(model); } foreach (var method in Methods) { method.Compile(model); } }
public override List<InstructionModel> GetInstructions(CompilationModel model) { var instructions = new List<InstructionModel>(); instructions.AddRange(LeftExpression.GetInstructions(model)); instructions.AddRange(RightExpression.GetInstructions(model)); instructions.Add(new InstructionModel(Instructions.IfIntNeqInstruction, "3")); instructions.Add(new InstructionModel(Instructions.PushIntInstruction, "1")); instructions.Add(new InstructionModel(Instructions.JumpInstruction, "2")); instructions.Add(new InstructionModel(Instructions.PushIntInstruction, "0")); instructions[instructions.Count - 3].Comment = model.GetComment(this); return instructions; }
public override List<InstructionModel> GetInstructions(CompilationModel model) { var trueBlockInstructions = TrueBlock.GetInstructions(model); var falseBlockInstructions = TrueBlock.GetInstructions(model); var jmpToFalse = (trueBlockInstructions.Count + 2).ToString(CultureInfo.InvariantCulture); // + 1 to following + 1 added jump var jmpOver = (falseBlockInstructions.Count + 1).ToString(CultureInfo.InvariantCulture); var instructions = new List<InstructionModel>(); instructions.Add(new InstructionModel(Instructions.PushIntInstruction, "1")); instructions.Latest().Comment = model.GetComment(this); instructions.AddRange(Expression.GetInstructions(model)); instructions.Add(new InstructionModel(Instructions.IfIntEqInstruction, jmpToFalse)); instructions.AddRange(trueBlockInstructions); instructions.Add(new InstructionModel(Instructions.JumpInstruction, jmpOver)); return instructions; }
public override List<InstructionModel> GetInstructions(CompilationModel model) { var method = FindParent<Method>(); var @class = (Class) method.Parent; var methodModel = model.GetClass(@class.Name).GetMethod(method.FullName); var index = methodModel.GetVariableIndex(Variable).ToString(CultureInfo.InvariantCulture); var type = methodModel.GetVariableType(Variable); var instructions = new List<InstructionModel>(); instructions.AddRange(Expression.GetInstructions(model)); // get instructions of expression instructions.Add(new InstructionModel(type == BuiltinTypes.Integer ? // store result into local variable Instructions.StoreIntInstruction : Instructions.StorePointerInstruction, index)); instructions.Latest().Comment = model.GetComment(this); return instructions; }
public override List<InstructionModel> GetInstructions(CompilationModel model) { var instructions = new List<InstructionModel>(); var method = FindParent<Method>(); var @classs = (Class) method.Parent; var methodModel = model.GetClass(@classs.Name).GetMethod(method.FullName); var type = methodModel.GetVariableType(Variable); var index = methodModel.GetVariableIndex(Variable).ToString(CultureInfo.InvariantCulture); if (type == BuiltinTypes.Integer) { instructions.Add(new InstructionModel(Instructions.LoadIntInstruction, index)); } else { instructions.Add(new InstructionModel(Instructions.LoadPointerInstruction, index)); } instructions.Latest().Comment = model.GetComment(this); return instructions; }
public override List<InstructionModel> GetInstructions(CompilationModel model) { var instructions = new List<InstructionModel>(); if (Expression != null) { instructions.AddRange(Expression.GetInstructions(model)); var type = Expression.GetExpressionType(model); var instruction = new InstructionModel(type == BuiltinTypes.Integer ? Instructions.ReturnIntInstruction : Instructions.ReturnPointerInstruction); instruction.Comment = model.GetComment(this); instructions.Add(instruction); } else { var instruction = new InstructionModel(Instructions.ReturnInstruction); instruction.Comment = model.GetComment(this); instructions.Add(instruction); } return instructions; }
public MethodModel GetMethodWithArgs(string name, Sequence <Expression> args, CompilationModel model) { var argsList = args.ToList(); MethodModel tmpMethod = null; MethodModel method = null; foreach (var m in Methods.Where(m => m.Name == name && m.Arguments.Count == argsList.Count)) { if (method == null) // to handle methods without parameters { method = m; } for (int i = 0; i < m.Arguments.Count; i++) { if (m.Arguments[i].Type == argsList[i].GetExpressionType(model) || m.Arguments[i].Type == BuiltinTypes.Any) { tmpMethod = m; } else { tmpMethod = null; } } if (tmpMethod != null) { method = tmpMethod; } } if (method != null) { return(method); } throw new Exception(string.Format("Method with with name {0} and {1} args not found!", name, argsList.Count)); }
public ClassModel(string name, CompilationModel model, bool skip = false) : this(name, "Any", model, skip) { }
public abstract string GetExpressionType(CompilationModel model);
public override string GetExpressionType(CompilationModel model) { return BuiltinTypes.Integer; }