PageRenderTime 18ms CodeModel.GetById 15ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/source/pseudo-backend/backend/pseudo/Backend.ooc

http://github.com/nddrylliog/oc
Unknown | 77 lines | 56 code | 21 blank | 0 comment | 0 complexity | ea35eb79e61a4c91448888690eab2e51 MD5 | raw file
 1use oc
 2
 3import ast/[Module, Node, FuncDecl, Access, Var, Scope, Type,
 4    Call, StringLit, NumberLit, Statement, Expression, Return, CoverDecl]
 5import middle/Resolver
 6
 7import structs/[HashMap, ArrayList, List]
 8
 9import frontend/BuildParams
10import ../Backend
11
12/**
13 * This backend outputs very simple pseudo-code: probably
14 * useful to debug compiler bugs at various stages
15 */
16
17CallBack: class {
18    f: Func (Node)
19
20    init: func(=f) {}
21}
22
23pseudo_Backend: class extends Backend {
24
25    process: func (module: Module, params: BuildParams) {
26	PseudoGenerator new(module, params)	
27    }
28
29}
30
31PseudoGenerator: class {
32
33    module: Module
34    params: BuildParams
35
36    map := HashMap<Class, CallBack> new()
37
38    init: func (=module, =params) {
39	// setup hooks
40	put(CoverDecl,  |cd| visitCoverDecl(cd as CoverDecl))
41
42	"-------------------------------------------------" println()
43	visitModule(module)
44	"-------------------------------------------------" println()
45    }
46
47    put: func (T: Class, f: Func (Node)) {
48	map put(T, CallBack new(f))
49    }
50
51    visitModule: func (m: Module) {
52	("module " + m fullName) println()
53
54	visitScope(m body)
55    }
56
57    visitScope: func (s: Scope) {
58	"{" println()
59	s body each(|stat| visitStat(stat))
60	"}" println()
61    }
62
63    visitStat: func (s: Statement) {
64	cb := map get(s class)
65	if(cb) {
66	    cb f(s)
67	} else {
68	    "Unsupported node type: %s" printfln(s class)
69	}
70    }
71
72    visitCoverDecl: func (cd: CoverDecl) {
73	"cover %s" printf(cd name)
74	visitScope(cd body)	
75    }
76
77}