Radicalc  Check-in [8af915788d]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:loads
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:8af915788d7e959b2803b6b3ea57aa10f47dd5eca8a36ff5e229d42890f472f7
User & Date: athaudia 2017-07-30 14:27:46
Context
2017-07-30
15:42
C output check-in: c979f00bf6 user: athaudia tags: trunk
14:27
loads check-in: 8af915788d user: athaudia tags: trunk
12:16
relational operators check-in: 2fd6968e53 user: athaudia tags: trunk
Changes

Changes to src/radicalc.js.













































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
var grammar = require("./parser.js");
var nearley = require("nearley");
var fs = require("fs");

function error(str) {
	console.log("error: " + str);
	process.exit(1);
}

var p = new nearley.Parser(grammar.ParserRules, grammar.ParserStart);

var src = fs.readFileSync("test.rcs", "utf8");
p.feed(src);

if(p.results == 0) {
	console.log("no parse found");
	process.exit(1);
}
else if(p.results > 1) {
	console.log("ambiguity found");
	process.exit(1);
}

var tree = p.results[0];
var funs = [];
var dataTypes = {
	void:{},
	i8:{},i16:{},i32:{},i64:{},
	u8:{},u16:{},u32:{},u64:{}
};

function getDataType(str) {
	if(dataTypes[str] == null) {
		console.log("error: Data type '" + str + "' doesn't exist");
		process.exit(-1);
	}
	return dataTypes[str];
}

function getFun(name, paramTypes) {
	for(var j = 0; j < funs.length; ++j) {
		var fun = funs[j];
		if(fun.name == name && fun.params.length == paramTypes.length) {
			var pe = true;
			for(var i = 0; i < fun.params.length; ++i)
				pe = pe && (fun.params[i].dataType == paramTypes[i]);
			if(pe)
				return fun;
		}
	}
	error("can't find function '"+ name + "'");
}

function preAnnotate() {
	tree.funs.forEach(function(fun) {
		fun.params = fun.params || [];
		fun.params.forEach(function(param) {
			param.dataType = getDataType(param.dataTypeStr);
			delete param.dataTypeStr;
		});
		fun.dataType = getDataType(fun.dataTypeStr);
		delete fun.dataTypeStr;
		funs.push(fun);
	});
}

function getVar(scope, name) {
	while(scope) {
		if(scope.vars[name])
			return scope.vars[name];
		scope = scope.parent;
	}
	error("can't find variable '"+name+"'");
}

function annotate(node) {
	switch(node.type) {
		case "assign":
			annotate(node.expr);
			node.dataType = node.expr.dataType;
			if(node.dataType != getVar(node.scope, node.name).dataType)
				error("trying to assign wrong data type");
			break;
		case "funCall":
			node.params.forEach(function(param){
				annotate(param);
			});
			node.fun = getFun(node.name, node.params.map(function(param) {
				return param.dataType;
			}));
			node.dataType = node.fun.dataType;
			break;
		case "litInt":
			node.dataType = dataTypes.i32;
			break;
		case "return":
			if(node.expr == null) {
				if(node.fun.dataType)
					error("can't void return in function with return type");
			}
			else {
				annotate(node.expr);
				node.dataType = node.expr.dataType;
				if(node.dataType != node.fun.dataType)
					error("wrong datatype in return");
			}
		case "varDef":
			if(node.dataType == null && node.expr == null)
				error("vardef has no datatype");
			if(node.expr)
				annotate(node.expr);
			if(node.dataTypeStr == null)
				node.dataType = node.expr.dataType;
			else
				node.dataType = getDataType(node.dataTypeStr);
			if(node.dataType == null)
				error("vardef has no datatype");
			break;
		case "varUse":
			node.var = getVar(node.scope, node.name);
			node.dataType = node.var.dataType;
			break;
		case "while":
			annotateBlock(node.block);
			break;
		default:
			error("not yet annotating '"+node.type+"'")
	}
}

function annotateBlock(block) {
	block.statements.forEach(function(stmt) {
		annotate(stmt);
	});
}

function annotateFunctions() {
	tree.funs.forEach(function(fun) {
		annotateBlock(fun.block);
	});
}

function initScope(node, scope, fun) {
	node.scope = scope;
	node.fun = fun;
	if(node.type == "varDef") {
		if(scope.vars[node.name])
			error("variable '"+node.name+"'already exists");
		scope.vars[node.name] = node;
	}
	else {
		if(node.params)
			node.params.forEach(function(param){initScope(param, scope)});
		if(node.expr)
			initScope(node.expr, scope, fun);
		
		if(node.block)
			initBlockScope(node.block, scope, fun);
		else if(node.ifBlock) {
			initBlockScope(node.ifBlock, scope, fun);
			if(node.elseBlock)
				initBlockScope(node.elseBlock, scope, fun);
		}
	}
}

function initBlockScope(block, parent, fun) {
	block.scope = {vars: {}, parent: parent};
	block.statements.forEach(function(statement) {
		initScope(statement, block.scope, fun);
	});
}

function initFunctionScopes() {
	tree.funs.forEach(function(fun) {
		initBlockScope(fun.block, null, fun);
	});
}

initFunctionScopes();
preAnnotate();
annotateFunctions();

Changes to src/radicalc.ne.

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

45

46
47
48
49
50
51
52
..
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
...
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208

FunDef -> "fun" __ ident _ "(" _ FunDefParams:? _ ")" _ Type:? NL
Block
"end" NL {%
	function(d) { return {
		name: d[2],
		params: d[6],
		typeStr: d[10],
		block: d[12]
	}}
%}

FunDefParams -> FunDefParam (_ "," _ FunDefParam):* {%
	function(d) { return [d[0]].concat(d[1].map(function(p) {return p[3]}))}
%}

FunDefParam -> ident _ Type {%
	function(d) { return {
		name: d[0],
		typeStr: d[2]
	}}
%}

Block -> (_ Stmt NL):* {%

	function(d) { return d[0].map(function(p){return p[1]})}

%}

Stmt ->
	IfStmt    {% id %}
|	WhileStmt {% id %}
|	Expr      {% function(d) {return {type: "stmtExpr", expr: d[0]}} %}
|	VarDef    {% id %}
................................................................................
%}

VarDef ->
	"var" __ ident _ (Type _):? "=" _ Expr {%
		function(d) { return {
			type: "varDef",
			name: d[2],
			typeStr: (d[4]||[null])[0],
			expr: d[7]
		}}
	%}
|	"var" _ ident _ Type {%
		function(d) { return {
			type: "varDef",
			name: d[2],
			typeStr: d[4],
			expr: null
		}}
	%}

Assign -> ident _ "=" _ Expr {%
	function(d) { return {
		type: "assign",
................................................................................
	%}
|	ParenExpr {% id %}

FunCall -> ident _ "(" _ FunCallParams:? _ ")" {%
	function(d) { return {
		type: "funCall",
		name: d[0],
		params: d[4]
	}}
%}

FunCallParams -> Expr (_ "," _ Expr):* {% 
	function(d) {return [d[0]].concat(d[1].map(function(p){return p[3]}))}
%}








|











|




>
|
>







 







|







|







 







|







21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
..
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
...
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210

FunDef -> "fun" __ ident _ "(" _ FunDefParams:? _ ")" _ Type:? NL
Block
"end" NL {%
	function(d) { return {
		name: d[2],
		params: d[6],
		dataTypeStr: d[10] || "void",
		block: d[12]
	}}
%}

FunDefParams -> FunDefParam (_ "," _ FunDefParam):* {%
	function(d) { return [d[0]].concat(d[1].map(function(p) {return p[3]}))}
%}

FunDefParam -> ident _ Type {%
	function(d) { return {
		name: d[0],
		dataTypeStr: d[2]
	}}
%}

Block -> (_ Stmt NL):* {%
	function(d) { return {
		statements: d[0].map(function(p){return p[1]})
	}}
%}

Stmt ->
	IfStmt    {% id %}
|	WhileStmt {% id %}
|	Expr      {% function(d) {return {type: "stmtExpr", expr: d[0]}} %}
|	VarDef    {% id %}
................................................................................
%}

VarDef ->
	"var" __ ident _ (Type _):? "=" _ Expr {%
		function(d) { return {
			type: "varDef",
			name: d[2],
			dataTypeStr: (d[4]||[null])[0],
			expr: d[7]
		}}
	%}
|	"var" _ ident _ Type {%
		function(d) { return {
			type: "varDef",
			name: d[2],
			dataTypeStr: d[4],
			expr: null
		}}
	%}

Assign -> ident _ "=" _ Expr {%
	function(d) { return {
		type: "assign",
................................................................................
	%}
|	ParenExpr {% id %}

FunCall -> ident _ "(" _ FunCallParams:? _ ")" {%
	function(d) { return {
		type: "funCall",
		name: d[0],
		params: d[4]||[]
	}}
%}

FunCallParams -> Expr (_ "," _ Expr):* {% 
	function(d) {return [d[0]].concat(d[1].map(function(p){return p[3]}))}
%}