public void Gather(TisLangParser.ProgramContext program) { int ip = 0; foreach (var expression in program.expression()) { var label = expression.label(); var instruction = expression.instruction(); var eol = expression.EOL(); if (label != null) { var name = label.SYMBOL().GetText().ToUpper(); Labels[name] = ip; } else if (instruction != null) { ip++; } else if (eol != null) { } else { throw new Exception("Unknown expression type"); } } }
public static List<IOperation> Compile(string assembly) { var stream = new AntlrInputStream(assembly); var lexer = new TisLangLexer(stream); var tokens = new CommonTokenStream(lexer); var parser = new TisLangParser(tokens); var program = parser.program(); if (parser.NumberOfSyntaxErrors > 0) { throw new Exception("Failed to parse program"); } var labels = new LabelGatherer(); labels.Gather(program); var operations = new OperationGatherer(); operations.Gather(program, labels.Labels); return operations.Operations; }
private object GetArgument(TisLangParser.ArgumentContext arg, Dictionary<string, int> labels) { var symbol = arg.SYMBOL(); var integer = arg.INTEGER(); if (symbol != null) { var name = symbol.GetText().ToUpper(); if (IsConnection(name)) { return new ConnectionRef(name); } else { return labels[name]; } } else if (integer != null) { return new ConstantRWRef(new ConstantRW(new Number(Int32.Parse(integer.GetText())))); } throw new NotImplementedException(); }
public void Gather(TisLangParser.ProgramContext program, Dictionary<string, int> labels) { foreach (var expression in program.expression()) { var label = expression.label(); var instruction = expression.instruction(); var eol = expression.EOL(); if (label != null) { } else if (instruction != null) { var name = instruction.SYMBOL().GetText(); var instructionType = GetInstructionType(name); var constructorArgumentCount = GetConstructorArgumentCount(instructionType); var rawArguments = instruction.argumentList().argument(); if (rawArguments.Count != constructorArgumentCount) { throw new Exception(string.Format("Instruction '{0}' takes exactly {1} arguments", name, constructorArgumentCount)); } var arguments = rawArguments.Select(arg => GetArgument(arg, labels)) .ToArray(); var op = (IOperation)Activator.CreateInstance(instructionType, arguments); Operations.Add(op); } else if (eol != null) { } else { throw new Exception("Unknown expression type"); } } }