Beispiel #1
0
        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");
                }
            }
        }
Beispiel #2
0
        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");
                }
            }
        }