Skip to content

Commit

Permalink
Update new grammar and AST
Browse files Browse the repository at this point in the history
  • Loading branch information
dirk committed Sep 12, 2015
1 parent 61fb4d4 commit fa89718
Show file tree
Hide file tree
Showing 3 changed files with 216 additions and 50 deletions.
22 changes: 19 additions & 3 deletions new-grammar.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
var fs = require('fs'),
util = require('util'),
jison = require('jison'),
prettyjson = require('prettyjson')
prettyjson = require('prettyjson'),
AST = require('./src/ast')

require('source-map-support').install()

function inspect(object) {
// console.log(util.inspect(object, {depth: 10}))
Expand All @@ -11,14 +14,27 @@ function inspect(object) {
var bnf = fs.readFileSync('src/grammar.jison', 'utf8'),
parser = new jison.Parser(bnf)

parser.yy = {
AST: AST,
binary: function (lexpr, op, rexpr) {
return new AST.Binary(lexpr, op, rexpr)
}
}

/*
var lines = [
"a(b.c)[0].d ",
"func e(f) { return f * 2 }",
"func e(f: G) { return f * 2 }",
"g = func () {}",
"let h = 1",
"if true { }",
"while false { }",
"for 1; 2; 3 { }",
]
*/

var file = fs.readFileSync('examples/fibonacci.hb', 'utf8')


inspect(parser.parse(lines.join("\n")))
var root = parser.parse(file)
root.dump()
54 changes: 49 additions & 5 deletions src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,13 @@ export class Binary extends Node {
toString(): string {
return this.lexpr.toString()+' '+this.op+' '+this.rexpr.toString()
}

dump() {
_win(`binary ${this.op}\n`)
this.lexpr.dump()
this.rexpr.dump()
_ind -= INDENT
}
}


Expand All @@ -235,7 +242,9 @@ export class Literal extends Node {
print(): void { out.write(this.toString()) }
toString(): string { return JSON.stringify(this.value) }

dump(): void { _w(this.toString()+"\n") }
dump(): void {
_w('literal '+this.toString()+"\n")
}
}


Expand Down Expand Up @@ -397,7 +406,7 @@ export class Function extends Node {
this.block = block
// Run some compiler checks
assertPropertyIsInstanceOf(this, 'args', Array)
assertSaneArgs(this.args)
// assertSaneArgs(this.args)
}
print(): void {
var args = this.inspectArgs()
Expand All @@ -409,9 +418,11 @@ export class Function extends Node {
var instance = this.type
if (this.ret) {
out.write('-> '+this.ret+' ')
} else {
} else if (instance) {
// If we computed an inferred return type for the type
out.write('-i> '+instance.type.ret.dump()+' ')
} else {
out.write('-> ? ')
}
this.block.print()
}
Expand All @@ -433,8 +444,10 @@ export class Function extends Node {
}

dump() {
var args = this.inspectArgs()
_win(`function (${args})\n`)
var args = this.inspectArgs(),
typ = this.type ? this.type.toString() : '?'
_win(`function (${args}) ${typ}\n`)
this.block.dump()
_ind -= INDENT
}
}
Expand Down Expand Up @@ -720,6 +733,13 @@ export class If extends Node {
this.elseBlock.print()
}
}

dump() {
_win("if\n")
this.cond.dump()
this.block.dump()
_ind -= INDENT
}
}


Expand Down Expand Up @@ -764,6 +784,25 @@ export class For extends Node {
_ind = i;
this.block.print()
}
dump() {
var self = this,
attrs = ['init', 'cond', 'after'];

_win("for\n")

for (var i = 0; i < attrs.length; i++) {
var attr = attrs[i]

if (self[attr]) {
_win(`.${attr}\n`)
self[attr].dump()
_ind -= INDENT
}
}

this.block.dump()
_ind -= INDENT
}
}


Expand Down Expand Up @@ -809,6 +848,11 @@ export class Return extends Node {
}
return 'return'
}
dump() {
_win("return\n")
if (this.expr) { this.expr.dump() }
_ind -= INDENT
}
}


Expand Down
Loading

0 comments on commit fa89718

Please sign in to comment.