Radicalc  Check-in [c979f00bf6]

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

Overview
Comment:C output
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:c979f00bf6b1aa746bda0d4abe30ce7008b8e9e3e82940535bf534670d43ebfa
User & Date: athaudia 2017-07-30 15:42:43
Context
2017-07-30
18:52
it compiles :o check-in: 7e142f1cb4 user: athaudia tags: trunk
15:42
C output check-in: c979f00bf6 user: athaudia tags: trunk
14:27
loads check-in: 8af915788d user: athaudia tags: trunk
Changes

Changes to src/radicalc.js.

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
..
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
...
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
...
170
171
172
173
174
175
176






177


















































































































178
179
180
181
182

	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);
	}
................................................................................
			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)
................................................................................
			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+"'")
	}
}

................................................................................
		initScope(statement, block.scope, fun);
	});
}

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






	});


















































































































}

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








|
|
|







 







|


|







 







|
|







 







>







 







>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>





>
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
..
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
...
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
...
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
	console.log("ambiguity found");
	process.exit(1);
}

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

function getDataType(str) {
	if(dataTypes[str] == null) {
		console.log("error: Data type '" + str + "' doesn't exist");
		process.exit(-1);
	}
................................................................................
			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.funCall = getFun(node.name, node.params.map(function(param) {
				return param.dataType;
			}));
			node.dataType = node.funCall.dataType;
			break;
		case "litInt":
			node.dataType = dataTypes.i32;
			break;
		case "return":
			if(node.expr == null) {
				if(node.fun.dataType)
................................................................................
			else {
				annotate(node.expr);
				node.dataType = node.expr.dataType;
				if(node.dataType != node.fun.dataType)
					error("wrong datatype in return");
			}
		case "varDef":
			if(node.dataTypeStr == null && node.expr == null)
				error("vardef of "+node.name+" 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":
			annotate(node.expr);
			annotateBlock(node.block);
			break;
		default:
			error("not yet annotating '"+node.type+"'")
	}
}

................................................................................
		initScope(statement, block.scope, fun);
	});
}

function initFunctionScopes() {
	tree.funs.forEach(function(fun) {
		initBlockScope(fun.block, null, fun);
		fun.params = fun.params || [];
		fun.params.forEach(function(param){
			if(fun.block.scope.vars[param.name])
				error("variable '"+param.name+"'already exists");
			fun.block.scope.vars[param.name] = param;
			param.isParam = true;
		});
	});
}

function cOutFunction(out, fun) {
	
}

function cOutDataType(out, dataType) {
	out.write(dataType.name);
}

function cOutFunName(out, fun) {
	out.write(fun.name);
	out.write("__");
	cOutDataType(out, fun.dataType);
	fun.params.forEach(function(param){
		out.write("__");
		cOutDataType(out, param.dataType);
	});
}

function cOutFunDef(out, fun) {
	cOutDataType(out, fun.dataType);
	out.write(" ");
	cOutFunName(out, fun);
	out.write("(");
	for(var i = 0; i < fun.params.length; ++i) {
		if(i > 0)
			out.write(", ");
		cOutDataType(out, fun.params[i].dataType);
		out.write(" ");
		out.write(fun.params[i].name);
	}
	out.write(")");
}

function cOutNode(out, node) {
	switch(node.type) {
		case "assign":
			out.write(node.name);
			out.write(" = ");
			cOutNode(out, node.expr);
			out.write(";\n");
			break;
		case "funCall":
			cOutFunName(out, node.funCall);
			out.write("(");
			for(var i = 0; i < node.params.length; ++i) {
				if(i > 0)
					out.write(", ");
				cOutNode(out, node.params[i]);
			}
			out.write(")");
			break;
		case "litInt":
			out.write(node.val);
			break;
		case "return":
			out.write("return ");
			if(node.expr)
				cOutNode(out, node.expr);
			out.write(";\n");
			break;
		case "varDef":
			if(node.expr) {
				out.write(node.name);
				out.write(" = ");
				cOutNode(out, node.expr);
				out.write(";\n");
			}
			break;
		case "varUse":
			out.write(node.name);
			break;
		case "while":
			out.write("while(");
			cOutNode(out, node.expr);
			out.write(")");
			cOutBlock(out, node.block);
			break;
		default:
			error("'"+node.type+"' not yet implemented in cOutNode");
	}
}

function cOutBlock(out, block) {
	out.write("{\n");
	for(var key in block.scope.vars) {
		var v = block.scope.vars[key];
		if(!v.isParam) {
			cOutDataType(out, v.dataType);
			out.write(" ");
			out.write(v.name);
			out.write(";\n");
		}
	}
	out.write("\n");
	block.statements.forEach(function(statement) {
		cOutNode(out, statement);
	});
	out.write("}\n");
}

function cOutFunctions(out) {
	funs.forEach(function(fun) {
		cOutFunDef(out, fun);
		out.write(";\n");
	});
	out.write("\n");
	funs.forEach(function(fun) {
		cOutFunDef(out, fun);
		cOutBlock(out, fun.block);
		out.write ("\n");
	});	
}

initFunctionScopes();
preAnnotate();
annotateFunctions();
cOutFunctions(process.stdout);

Changes to src/radicalc.ne.

63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
		elseBlock: d[6]?d[6][3]:null
	}}
%}

WhileStmt -> "while" _ Expr _ NL Block _ "end" {%
	function(d) { return {
		type: "while",
		cond: d[2],
		block: d[5]
	}}
%}

VarDef ->
	"var" __ ident _ (Type _):? "=" _ Expr {%
		function(d) { return {







|







63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
		elseBlock: d[6]?d[6][3]:null
	}}
%}

WhileStmt -> "while" _ Expr _ NL Block _ "end" {%
	function(d) { return {
		type: "while",
		expr: d[2],
		block: d[5]
	}}
%}

VarDef ->
	"var" __ ident _ (Type _):? "=" _ Expr {%
		function(d) { return {