public override CompilerResult VisitExpression([NotNull] LangParser.ExpressionContext context) { if (context.NAME() != null) { string name = context.NAME().GetText(); Parameter parameter = CurrContext().lookupParameter(name); LLVMValueRef? @ref = null; if (parameter != null) { @ref = parameter.Load(); } else { LocalVariable lv = CurrContext().lookupLocalVar(name); if (lv != null) { @ref = lv.Load(builder); } } if ([email protected]) { throw new Exception("Lookup failed " + name); } return(new Int32CompilerResult(@ref.Value)); } if (context.NUMBER() != null) { return(new Int32CompilerResult(LLVM.ConstInt(LLVM.Int32Type(), UInt32.Parse(context.NUMBER().GetText()), true))); } if (context.ChildCount == 3) { Int32CompilerResult lhs = (Int32CompilerResult)VisitExpression(context.expression()[0]); Int32CompilerResult rhs = (Int32CompilerResult)VisitExpression(context.expression()[1]); if (context.MULTIPLY() != null) { return(new Int32CompilerResult(LLVM.BuildMul(builder, lhs.reference, rhs.reference, "multmp"))); } if (context.DIVIDE() != null) { return(new Int32CompilerResult(LLVM.BuildSDiv(builder, lhs.reference, rhs.reference, "divtmp"))); } if (context.MODULO() != null) { return(new Int32CompilerResult(LLVM.BuildSRem(builder, lhs.reference, rhs.reference, "remtmp"))); } if (context.ADD() != null) { return(new Int32CompilerResult(LLVM.BuildAdd(builder, lhs.reference, rhs.reference, "addtmp"))); } if (context.SUBSTRACT() != null) { return(new Int32CompilerResult(LLVM.BuildSub(builder, lhs.reference, rhs.reference, "subtmp"))); } } return(base.VisitExpression(context)); }
public static Ast.Expression Transform(LangParser.ExpressionContext ctx) { var name = ctx.NAME(); var exprs = ctx.expression(); // function call if (name != null) { var expressions = new Ast.Expression[exprs.Length]; for (uint i = 0; i < exprs.Length; i++) { expressions[i] = Transform(exprs[i]); } return(new Ast.FunctionCall(name.GetText(), expressions)); } var atom = ctx.atom(); if (ctx.LBRACK() != null || atom != null) { Ast.Expression transformed; if (atom != null) { transformed = Transform(atom); } else { transformed = Transform(exprs[0]); } if (ctx.ADD() != null) { return(new Ast.UnaryOperator(transformed, Ast.UnaryOperator.Operation.PLS)); } if (ctx.SUB() != null) { return(new Ast.UnaryOperator(transformed, Ast.UnaryOperator.Operation.NEG)); } return(transformed); } if (exprs.Length == 2) { var mul = ctx.MULPRIOR(); if (mul != null) { var asStr = mul.GetText(); if (asStr == "*") { return(new Ast.BinaryOperator(Transform(exprs[0]), Transform(exprs[1]), Ast.BinaryOperator.Operation.MUL)); } if (asStr == "/") { return(new Ast.BinaryOperator(Transform(exprs[0]), Transform(exprs[1]), Ast.BinaryOperator.Operation.DIV)); } } var rel = ctx.RELATIONPRIOR(); if (rel != null) { var asStr = rel.GetText(); if (asStr == "<") { return(new Ast.BinaryOperator(Transform(exprs[0]), Transform(exprs[1]), Ast.BinaryOperator.Operation.ROL)); } if (asStr == ">") { return(new Ast.BinaryOperator(Transform(exprs[0]), Transform(exprs[1]), Ast.BinaryOperator.Operation.ROG)); } if (asStr == "==") { return(new Ast.BinaryOperator(Transform(exprs[0]), Transform(exprs[1]), Ast.BinaryOperator.Operation.ROE)); } if (asStr == "!=") { return(new Ast.BinaryOperator(Transform(exprs[0]), Transform(exprs[1]), Ast.BinaryOperator.Operation.RONE)); } throw new NotSupportedException("Kernel error : unsupported relation type"); } if (ctx.OPAND() != null) { return(new Ast.BinaryOperator(Transform(exprs[0]), Transform(exprs[1]), Ast.BinaryOperator.Operation.AND)); } if (ctx.OPOR() != null) { return(new Ast.BinaryOperator(Transform(exprs[0]), Transform(exprs[1]), Ast.BinaryOperator.Operation.OR)); } if (ctx.ADD() != null) { return(new Ast.BinaryOperator(Transform(exprs[0]), Transform(exprs[1]), Ast.BinaryOperator.Operation.ADD)); } if (ctx.SUB() != null) { return(new Ast.BinaryOperator(Transform(exprs[0]), Transform(exprs[1]), Ast.BinaryOperator.Operation.SUB)); } } throw new NotSupportedException("Kernel error : unsupported expression type"); }