Skip to content

Commit

Permalink
Use inquisitor pattern for pseudo-backend
Browse files Browse the repository at this point in the history
  • Loading branch information
nddrylliog committed Dec 9, 2013
1 parent 7791848 commit 45934fe
Show file tree
Hide file tree
Showing 13 changed files with 100 additions and 30 deletions.
5 changes: 5 additions & 0 deletions source/oc/ast/Access.ooc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@


import oc/middle/Resolver
import Inquisitor
import Expression, Type, Var, Node, Scope, Statement, FuncDecl

Access: class extends Expression {
Expand Down Expand Up @@ -48,4 +49,8 @@ Access: class extends Expression {
if(marker && !ref global) marker markAccess(this)
}

surrender: func (inq: Inquisitor) {
inq visitAccess(this)
}

}
6 changes: 6 additions & 0 deletions source/oc/ast/Call.ooc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import structs/[List, ArrayList]

import oc/middle/Resolver

import Inquisitor
import FuncDecl, Expression, Access, Type

Call: class extends Expression {
Expand Down Expand Up @@ -33,4 +35,8 @@ Call: class extends Expression {
null
}

surrender: func (inq: Inquisitor) {
inq visitCall(this)
}

}
6 changes: 6 additions & 0 deletions source/oc/ast/FuncDecl.ooc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import structs/[ArrayList, HashMap]

import oc/middle/Resolver

import Inquisitor
import Expression, Statement, Scope, Var, Type, Access, Return, Call

FuncDecl: class extends Expression {
Expand Down Expand Up @@ -149,4 +151,8 @@ FuncDecl: class extends Expression {
_type
}

surrender: func (inq: Inquisitor) {
inq visitFuncDecl(this)
}

}
5 changes: 5 additions & 0 deletions source/oc/ast/Import.ooc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

import Inquisitor
import Node, Module

/**
Expand All @@ -15,4 +16,8 @@ Import: class extends Node {

init: func (=importName) {}

surrender: func (inq: Inquisitor) {
inq visitImport(this)
}

}
35 changes: 35 additions & 0 deletions source/oc/ast/Inquisitor.ooc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

// branches
import oc/ast/[Node, Expression, Statement]

// leafs
import oc/ast/[Access, Call, CoverDecl, FuncDecl, Import, Module, NumberLit,
Return, Scope, StringLit, Type, Var]

Inquisitor: abstract class {

visitNode: func (node: Node) {
if (!node) return
node surrender(this)
}

visitModule: func (m: Module)
visitImport: func (i: Import)

visitCoverDecl: func (cd: CoverDecl)
visitFuncDecl: func (e: FuncDecl)
visitVar: func (v: Var)

visitScope: func (s: Scope)

visitAccess: func (a: Access)
visitCall: func (c: Call)
visitReturn: func (r: Return)

visitNumberLit: func (nl: NumberLit)
visitStringLit: func (sl: StringLit)

visitBaseType: func (bt: BaseType)

}

5 changes: 5 additions & 0 deletions source/oc/ast/Node.ooc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

import oc/middle/Resolver
import Inquisitor

import Access, Var // for resolveAccess

Expand All @@ -21,4 +22,8 @@ Node: class {
// <your ad here>
}

surrender: func (inq: Inquisitor) {
raise("#{class name} surrender(): stub!")
}

}
6 changes: 6 additions & 0 deletions source/oc/ast/NumberLit.ooc
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

import Expression, Type

import Inquisitor
import oc/middle/Resolver

/** Different number formats - in sync with nagaqueen's "IntFormat" */
Expand Down Expand Up @@ -33,4 +35,8 @@ NumberLit: class extends Expression {
value
}

surrender: func (inq: Inquisitor) {
inq visitNumberLit(this)
}

}
6 changes: 6 additions & 0 deletions source/oc/ast/Return.ooc
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

import oc/middle/Resolver

import Inquisitor
import Statement, Expression

Return: class extends Statement {
Expand All @@ -16,4 +18,8 @@ Return: class extends Statement {
expr ? "return " + expr toString() : "return"
}

surrender: func (inq: Inquisitor) {
inq visitReturn(this)
}

}
6 changes: 6 additions & 0 deletions source/oc/ast/Scope.ooc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import structs/[ArrayList, List]

import oc/middle/Resolver

import Inquisitor
import Node, Statement, Var, Access

Scope: class extends Node {
Expand Down Expand Up @@ -61,4 +63,8 @@ Scope: class extends Node {
"{}"
}

surrender: func (inq: Inquisitor) {
inq visitScope(this)
}

}
6 changes: 6 additions & 0 deletions source/oc/ast/StringLit.ooc
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

import Expression, Type

import Inquisitor
import oc/middle/Resolver

StringLit: class extends Expression {
Expand All @@ -19,4 +21,8 @@ StringLit: class extends Expression {
"\"" + value + "\""
}

surrender: func (inq: Inquisitor) {
inq visitStringLit(this)
}

}
6 changes: 6 additions & 0 deletions source/oc/ast/Type.ooc
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@


import oc/middle/Resolver

import Inquisitor
import Node, FuncDecl

Type: abstract class extends Node {
Expand All @@ -23,6 +25,10 @@ BaseType: class extends Type {

toString: func -> String { name }

surrender: func (inq: Inquisitor) {
inq visitBaseType(this)
}

}

VoidType: class extends BaseType {
Expand Down
6 changes: 6 additions & 0 deletions source/oc/ast/Var.ooc
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@


import oc/middle/Resolver

import Inquisitor
import Type, Expression

Var: class extends Expression {
Expand Down Expand Up @@ -36,4 +38,8 @@ Var: class extends Expression {
name + (type ? ": " + type toString() : " :") + (expr ? "= " + expr toString() : "")
}

surrender: func (inq: Inquisitor) {
inq visitVar(this)
}

}
32 changes: 2 additions & 30 deletions source/oc/backend/pseudo/PseudoBackend.ooc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use oc
import structs/[HashMap, ArrayList, List]

// ours
import oc/ast/[Inquisitor]
import oc/ast/[Module, Node, FuncDecl, Access, Var, Scope, Type,
Call, StringLit, NumberLit, Statement, Expression, Return, CoverDecl]
import oc/middle/Resolver
Expand Down Expand Up @@ -31,33 +32,17 @@ pseudo_Backend: class extends Backend {

}

PseudoGenerator: class {
PseudoGenerator: class extends Inquisitor {

module: Module
params: BuildParams

map := HashMap<Class, CallBack> new()

init: func (=module, =params) {
// setup hooks
put(CoverDecl, |cd| visitCoverDecl(cd as CoverDecl))
put(FuncDecl, |fd| visitFuncDecl(fd as FuncDecl))
put(Var, |v| visitVar(v as Var))
put(Scope, |s| visitScope(s as Scope))
put(Call, |c| visitCall(c as Call))
put(BaseType, |bt| visitBaseType(bt as BaseType))
put(Access, |a| visitAccess(a as Access))
put(NumberLit, |nl| visitNumberLit(nl as NumberLit))

"-------------------------------------------------" println()
visitModule(module)
"-------------------------------------------------" println()
}

put: func (T: Class, f: Func (Node)) {
map put(T, CallBack new(f))
}

visitModule: func (m: Module) {
"module #{m fullName}" println()

Expand Down Expand Up @@ -92,19 +77,6 @@ PseudoGenerator: class {
visitScope(cd body)
}

visitNode: func (n: Node) {
if (!n) {
"(nil)" print()
return
}
cb := map get(n class)
if(cb) {
cb f(n)
} else {
err("Unsupported node type: #{n class name}")
}
}

visitFuncDecl: func (fd: FuncDecl) {
if (fd name && !fd name empty?()) {
fd name print()
Expand Down

0 comments on commit 45934fe

Please sign in to comment.