# HG changeset patch # User Anders Halager # Date 1208515854 -7200 # Node ID 2ce5209f1954bf19013c14740c7fd7e85857ebaa # Parent 606a57c90a0b81c529ab185f4f5c8510bf468339 Starting to work on bool support, for now == works diff -r 606a57c90a0b -r 2ce5209f1954 ast/Exp.d --- a/ast/Exp.d Fri Apr 18 12:24:14 2008 +0200 +++ b/ast/Exp.d Fri Apr 18 12:50:54 2008 +0200 @@ -55,10 +55,11 @@ class BinaryExp : Exp { - public enum Operator : char + public enum Operator { - Mul = '*', Div = '/', - Add = '+', Sub = '-' + Eq, Ne, + Mul, Div, + Add, Sub, } this(Operator op, Exp left, Exp right) @@ -69,6 +70,13 @@ this.right = right; } + char[] resultType() + { + if (op >= Operator.Eq && op <= Operator.Ne) + return "bool"; + return null; + } + Operator op; Exp left, right; } diff -r 606a57c90a0b -r 2ce5209f1954 gen/LLVMGen.d --- a/gen/LLVMGen.d Fri Apr 18 12:24:14 2008 +0200 +++ b/gen/LLVMGen.d Fri Apr 18 12:50:54 2008 +0200 @@ -23,14 +23,18 @@ "byte" : "i8", "short" : "i16", "long" : "i64", - "bool" : "i8", + "bool" : "i1", "float" : "float", "double" : "double", - "void" : "void", - "+" : "add", - "-" : "sub", - "*" : "mul", - "/" : "div" + "void" : "void" + ]; + alias BinaryExp.Operator op; + opToLLVM = [ + op.Add : "add"[], + op.Sub : "sub", + op.Mul : "mul", + op.Div : "div", + op.Eq : "icmp eq" ]; table = new SimpleSymbolTable(); } @@ -191,10 +195,16 @@ auto res = Ref(left.type, table.find); printBeginLine(res.name); - print(" = "~typeToLLVM[[binaryExp.op]]~" "); + print(" = "~opToLLVM[binaryExp.op]~" "); print(left); print(", "); printEndLine(right.name); + + // exp always returns known type (== returns bool no matter + // what the params are) + if (binaryExp.resultType) + res.type = typeToLLVM[binaryExp.resultType]; + return res; case ExpType.IntegerLit: auto integetLit = cast(IntegerLit)exp; @@ -417,8 +427,9 @@ SimpleSymbolTable table; SymbolTable symbolTable; static char[][char[]] typeToLLVM; + static char[][BinaryExp.Operator] opToLLVM; - static char[][] intTypes = [ "i8", "i16", "i32", "i64" ]; + static char[][] intTypes = [ "i1", "i8", "i16", "i32", "i64" ]; } struct Ref diff -r 606a57c90a0b -r 2ce5209f1954 parser/Parser.d --- a/parser/Parser.d Fri Apr 18 12:24:14 2008 +0200 +++ b/parser/Parser.d Fri Apr 18 12:50:54 2008 +0200 @@ -294,10 +294,12 @@ static BinOp[] _binary = [ - {Tok.Add, 3, true, BinaryExp.Operator.Add}, - {Tok.Sub, 3, true, BinaryExp.Operator.Sub}, - {Tok.Mul, 5, true, BinaryExp.Operator.Mul}, - {Tok.Div, 5, true, BinaryExp.Operator.Div} + {Tok.Equals, 2, true, BinaryExp.Operator.Eq}, + {Tok.NotEquals, 2, true, BinaryExp.Operator.Ne}, + {Tok.Add, 3, true, BinaryExp.Operator.Add}, + {Tok.Sub, 3, true, BinaryExp.Operator.Sub}, + {Tok.Mul, 5, true, BinaryExp.Operator.Mul}, + {Tok.Div, 5, true, BinaryExp.Operator.Div} ]; BinOp* binary(Tok t) { diff -r 606a57c90a0b -r 2ce5209f1954 test.td --- a/test.td Fri Apr 18 12:24:14 2008 +0200 +++ b/test.td Fri Apr 18 12:50:54 2008 +0200 @@ -8,12 +8,18 @@ return nice(var1, var2); } +int fac(int n) +{ + if (n - 1) + return n * fac(n - 1); + return 1; +} + int nice(long s, short t) { byte x = 5 + t; if (x == 0) - if (s) - t = 5 + 1 * 5 * s + t; + t = 5 + 1 * 5 * s + t; return 2 * (t + -1) - x; }