public MethodDeclaration(ASTNode?node, IMnemonicsCompiler compiler) : base(node) { var nameSpace = node.GetChildAndAddError(0, "namespace", compiler) .GetChildAndAddError(0, "LITERAL", compiler)?.Value; var method_name = node.GetChildAndAddError(2, "method_name", compiler) .GetChildAndAddError(0, "LITERAL", compiler)?.Value; var body = node.GetChildAndAddError(node.Value.Children.Count - 1, "block_of_lopla", compiler); var code = compiler.Get(body); var method_parameters = node.GetChildAndAddError(4, "method_parameters", compiler, true); var methodArguments = new List <string>(); if (method_parameters != null && method_parameters.Value.Children.Any()) { foreach (var valueChild in method_parameters.Value.Children) { var name = valueChild.GetChildAndAddError(0, "LITERAL", compiler).Value.Value; methodArguments.Add(name); } } Pointer = new MethodPointer { Name = method_name, NameSpace = nameSpace }; Code = new Method { ArgumentList = methodArguments, Code = new List <Mnemonic> { code } }; }
public ExpressionCommon(ASTNode?node, IMnemonicsCompiler runtime) : base(node) { Arguments = new Arguments(); if (node?.Children.Count == 3) { var left = node?.Children[0]; var middle = node?.Children[1]; var right = node?.Children[2]; var leftExpression = runtime.Get(left); var operatorKind = middle.Value.Value; var rightExpression = runtime.Get(right); var o = new Operator(); if (Expression.Operators.ContainsKey(operatorKind)) { o.Kind = Expression.Operators[operatorKind]; } else { runtime.AddError( new CompilationError($"Operator {operatorKind} is not discovered by compiler.")); } Arguments.Args.Add(leftExpression); Arguments.Args.Add(o); Arguments.Args.Add(rightExpression); } else if (node?.Children.Count == 1) { Arguments.Args.Add(runtime.Get(node.Value.Children[0])); } }
public VariableName(ASTNode?node, IMnemonicsCompiler runtime) : base(node) { Pointer = new VariablePointer { Name = node.GetChildAndAddError(0, "LITERAL", runtime)?.Value }; }
public MethodCall(ASTNode?node, IMnemonicsCompiler runtime) : base(node) { var namespaceName = node.GetChildAndAddError(0, "namespace", runtime) .GetChildAndAddError(0, "LITERAL", runtime, false); var methodName = node.GetChildAndAddError(2, "method_name", runtime) .GetChildAndAddError(0, "LITERAL", runtime, false); var methodArguments = node.GetChildAndAddError(4, "method_arguments", runtime, true); if (namespaceName.HasValue && methodName.HasValue) { Pointer = new MethodPointer { NameSpace = namespaceName.Value.Value, Name = methodName.Value.Value }; Arguments = new Arguments(); if (methodArguments.HasValue) { for (var k = 0; k < methodArguments.Value.Children.Count; k += 2) { var element = methodArguments.Value.Children.ElementAt(k); var value = runtime.Get(element); Arguments.Args.Add(value); } } } }
public Assigment(ASTNode?node, IMnemonicsCompiler runtime) : base(node) { var variableNameNode = node.GetChildAndAddError(0, "variable_name", runtime, true); var varValueTable = node.GetChildAndAddError(0, "var_value_table", runtime, true); if (variableNameNode.HasValue) { LeftSide = new VariableName(variableNameNode, runtime); } else if (varValueTable.HasValue) { LeftSide = new ValueTable(varValueTable, runtime); } else { runtime.AddError(new CompilationError( "Failed to find variable name in left side of assigment.")); } var expression = runtime.Get(node.Value.Children[2]); RightSide = expression; }
public Block(ASTNode?node, IMnemonicsCompiler runtime) : base(node) { foreach (var valueChild in node.Value.Children) { var mnemonic = runtime.Get(valueChild); Lines.Add(mnemonic); } }
public ExpressionBracket(ASTNode?node, IMnemonicsCompiler runtime) : base(node) { if (node?.Children.Count == 1) { var expresionToken = node?.Children[0]; SubExpresion = runtime.Get(expresionToken); } }
public If(ASTNode?node, IMnemonicsCompiler runtime) : base(node) { var logicalTest = node.GetChildAndAddError(1, "expression", runtime, false); Condition = runtime.Get(logicalTest); var blockOfLopla = node.GetChildAndAddError(2, "block_of_lopla", runtime, false); BlockOfCode = runtime.Get(blockOfLopla); }
public ValueTable(ASTNode?node, IMnemonicsCompiler runtime) : base(node) { TablePointer = new VariablePointer { Name = node .GetChildAndAddError(0, "variable_name", runtime)? .GetChildAndAddError(0, "LITERAL", runtime)?.Value }; ElementPositionInTable = runtime.Get(node.Value.Children[1]); }
public ExpressionPrefix(ASTNode?node, IMnemonicsCompiler runtime) : base(node) { if (node?.Children.Count == 2) { var right = node?.Children[1]; this.MultByMinus1 = runtime.Get(right); } else { this.ExpressionToPrefix = runtime.Get(node.Value.Children[0]); } }
public DeclareTable(ASTNode?node, IMnemonicsCompiler runtime) : base(node) { Values = new List <Mnemonic>(); var kids = node.GetChildAndAddError(0, "declare_table_values", runtime, true); if (kids.HasValue && kids.Value.Children.Count > 0) { foreach (var valueChild in kids.Value.Children) { Values.Add(runtime.Get(valueChild)); } } }
public Return(ASTNode?node, IMnemonicsCompiler runtime) : base(node) { var returnValue = node.GetChildAndAddError(1, "expression", runtime, true); if (returnValue == null) { ReturnExpression = new Nop(node); } else { ReturnExpression = runtime.Get(returnValue); } }
public static ASTNode?GetChildAndAddError(this ASTNode?node, int i, string tokenName, IMnemonicsCompiler runtime, bool optional = false) { if (node != null) { if (node.Value.Children.Count >= i + 1) { var element = node.Value.Children.ElementAt(i); if (element.Symbol.Name != tokenName) { if (!optional) { runtime.AddError(new CompilationError($"Expected: {tokenName} but found: {element.Symbol.Name}.")); } } else { return(element); } } else if (!optional) { runtime.AddError(new CompilationError($"Expected: {i} tokens but found: {node.Value.Children.Count} when looking for: {tokenName}.")); } } else { if (!optional) { runtime.AddError(new CompilationError($"Parent node for: {tokenName} not found.")); } } return(null); }
public static ASTNode?GetChildAndAddError(this ASTNode node, int i, string tokenName, IMnemonicsCompiler runtime, bool optional = false) { ASTNode?n = node; return(n.GetChildAndAddError(i, tokenName, runtime, optional)); }
public Expression(ASTNode?node, IMnemonicsCompiler runtime) : base(node) { this.SubExpression = runtime.Get(node.Value.Children[0]); }