Mercurial > projects > dang
diff parser/Parser.d @ 48:b6c1dc30ca4b new_gen
Only tests that dont pass now are structs and switches
As far as the parser is concerned assignments are binary expressions
Fixed a bug in codegen of if's - it is important to remember that the
builder might be positioned at a new block after generating sub-statements
author | Anders Halager <halager@gmail.com> |
---|---|
date | Thu, 24 Apr 2008 19:42:53 +0200 |
parents | b0a691727a0c |
children | c96cdcbdb9d6 |
line wrap: on
line diff
--- a/parser/Parser.d Wed Apr 23 17:09:44 2008 +0200 +++ b/parser/Parser.d Thu Apr 24 19:42:53 2008 +0200 @@ -29,12 +29,12 @@ Decl[] declarations; while(lexer.peek.type != Tok.EOF) - declarations ~= parseRootDecl(); + declarations ~= parseDecl(); return declarations; } - Decl parseRootDecl() + Decl parseDecl() { Token t = lexer.peek; @@ -127,21 +127,57 @@ Stmt bodyStmt = parseSingleOrCompoundStatement(); return action.actOnWhileStmt(_while, cond, bodyStmt); + /* + One of four things: + A declaration of a function/variable `type id ...` + A direct assignment `id = exp;` + An indirect assignment `id.id = exp` + Some sort of free standing expression + + The assignments should be handled as binary expressions? + */ case Tok.Identifier: - return null; + Token iden = lexer.peek; + Token n = lexer.peek(1); + // Must be an decl, if we start with a basic type, or two + // identifiers in a row + if (iden.isBasicType() || n.isIdentifier()) + { + // manually hardcoded to only support "type id [= exp];" + // as that is the only thing the codegen understands + Id type = Id(lexer.next); + Id id = Id(lexer.next); + Exp init; + if (skip(Tok.Assign)) + init = parseExpression(); + require(Tok.Seperator); + Decl d = action.actOnDeclarator(type, id, init); + return action.actOnDeclStmt(d); + } + // Expression: a.b, a = b, a(b) etc. + else + { + Exp exp = parseExpression(); + require(Tok.Seperator); + return action.actOnExprStmt(exp); + } + break; case Tok.Switch: + throw error(__LINE__, ":(").tok(lexer.peek); return null; default: - return null; + if (t.isBasicType()) + goto case Tok.Identifier; + throw error(__LINE__, ":(").tok(lexer.peek); } - error(__LINE__, "").tok(t); + throw error(__LINE__, "").tok(t); return null; } /** - Parses a function/method given the already parsed + Parses a function/method given the already parsed return type and name */ Decl parseFunc(ref Id type, ref Id name) { @@ -150,8 +186,7 @@ Stmt stmt = parseCompoundStatement(); - action.actOnEndOfFunction(func, stmt); - return func; + return action.actOnEndOfFunction(func, stmt); } /** @@ -282,21 +317,18 @@ Exp iden = parseExpIdentifier(value); switch(lexer.peek.type) { - // TODO: Function calls are parsed but ignored case Tok.OpenParentheses: - lexer.next; - Exp[] args; + Token lp = lexer.next; + SmallArray!(Exp, 8) args; while(lexer.peek.type != Tok.CloseParentheses) { if(lexer.peek.type == Tok.Comma) - { lexer.next; - } args ~= parseExpression(); } - lexer.next(); - return null;//new CallExp(iden, args); + Token rp = lexer.next(); + return action.actOnCallExpr(iden, lp, args.unsafe(), rp); default: return iden; @@ -334,6 +366,8 @@ static const BinOp[] _binary = [ + {Tok.Assign, 1, false, Operator.Assign}, + {Tok.Eq, 2, true, Operator.Eq}, {Tok.Ne, 2, true, Operator.Ne},