diff options
Diffstat (limited to 'parser.d')
| -rw-r--r-- | parser.d | 46 |
1 files changed, 46 insertions, 0 deletions
@@ -12,6 +12,7 @@ enum FormType { NIL, SYMBOL, FUNC, + LAMBDA, DEF, BLOCK, IF, @@ -211,6 +212,21 @@ class Func : Form { } } +class Lambda : Form { + + Cons args; + Form[] lambdaBody; + + this (int line) { + this.line = line; + this.type = FormType.LAMBDA; + } + + void addToBody(Form f) { + this.lambdaBody ~= f; + } +} + class Def : Form { Symbol name; @@ -449,6 +465,34 @@ class Parser { return def; } + Form parseLambda() { + // we've parsed `lambda` already + Form args = parseForm(); + if (args.type != FormType.CONS && args.type != FormType.NIL) { + return new ParseError("func definitions expect a list of arguments", line); + } + + Lambda lamb = new Lambda(line); + lamb.args = cast(Cons)args; + + char next; + while(peekable()) { + next = peek(); + if (next == ')') { + break; + } + lamb.addToBody(this.parseForm()); + } + + if (!peekable()) { + return new ParseError("unterminated lambda", line); + } + + advance(); // consume closing paren + + return lamb; + } + Form parseFunc() { // we've parsed `func`, but not the symbol yet Form sym = parseForm(); @@ -579,6 +623,8 @@ class Parser { switch (s.name) { case "func": return parseFunc(); + case "lambda": + return parseLambda(); case "def": return parseDef(); case "block": |
