Radicalc  Check-in [7e142f1cb4]

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

Overview
Comment:it compiles :o
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:7e142f1cb42be8507572bb235e229cab692ce6b0bb07917220b447d3f5028660
User & Date: athaudia 2017-07-30 18:52:30
Context
2017-08-01
00:59
compiling with llvm? check-in: 5ca6304291 user: athaudia tags: trunk
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
Changes

Changes to src/radicalc.js.

1
2
3

4
5
6
7
8
9
10
...
100
101
102
103
104
105
106



107
108
109
110
111
112
113
...
133
134
135
136
137
138
139

140
141
142
143
144
145
146
147
...
170
171
172
173
174
175
176


177
178
179
180
181
182
183
...
186
187
188
189
190
191
192





193
194
195
196

197
198
199
200
201
202
203
204
205
206
207
208
209
...
241
242
243
244
245
246
247




248
249
250
251
252
253
254
...
282
283
284
285
286
287
288

289

290
291

292
293
294

295
296
297

298




299
300
301
302
303

304








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);
................................................................................
			}
			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;
................................................................................
	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") {
................................................................................
	block.statements.forEach(function(statement) {
		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);
................................................................................
			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");
			}
................................................................................
	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);











>







 







>
>
>







 







>
|







 







>
>







 







>
>
>
>
>




>


|


|







 







>
>
>
>







 







>

>
|
|
>



>
|
|
|
>
|
>
>
>
>





>
|
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
...
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
...
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
...
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
...
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
...
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
...
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
var grammar = require("./parser.js");
var nearley = require("nearley");
var fs = require("fs");
const { exec } = require('child_process');

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

var p = new nearley.Parser(grammar.ParserRules, grammar.ParserStart);
................................................................................
			}
			else {
				annotate(node.expr);
				node.dataType = node.expr.dataType;
				if(node.dataType != node.fun.dataType)
					error("wrong datatype in return");
			}
		case "stmtExpr":
			annotate(node.expr);
			break;
		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;
................................................................................
	block.statements.forEach(function(stmt) {
		annotate(stmt);
	});
}

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

function initScope(node, scope, fun) {
	node.scope = scope;
	node.fun = fun;
	if(node.type == "varDef") {
................................................................................
	block.statements.forEach(function(statement) {
		initScope(statement, block.scope, fun);
	});
}

function initFunctionScopes() {
	tree.funs.forEach(function(fun) {
		if(fun.isStub)
			return;
		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("rcst__");
	out.write(dataType.name);
}

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

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

function cOutFunDef(out, fun) {
	cOutDataType(out, fun.dataType);
	out.write(" ");
	cOutFunName(out, fun);
................................................................................
			break;
		case "return":
			out.write("return ");
			if(node.expr)
				cOutNode(out, node.expr);
			out.write(";\n");
			break;
		case "stmtExpr":
			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");
			}
................................................................................
	block.statements.forEach(function(statement) {
		cOutNode(out, statement);
	});
	out.write("}\n");
}

function cOutFunctions(out) {
	out.write("#include \"rcs_std.h\"\n\n");
	funs.forEach(function(fun) {
		if(!fun.isStub) {
			cOutFunDef(out, fun);
			out.write(";\n");
		}
	});
	out.write("\n");
	funs.forEach(function(fun) {
		if(!fun.isStub) {
			cOutFunDef(out, fun);
			cOutBlock(out, fun.block);
			out.write ("\n");
		}
	});
	var main = getFun("main", []);
	out.write("int main() {");
	cOutFunName(out, main);
	out.write("();}\n");
}

initFunctionScopes();
preAnnotate();
annotateFunctions();
var out = fs.createWriteStream("tmp.c");
cOutFunctions(out);
out.end();
exec('C:\\apps\\tcc\\tcc.exe -o test.exe tmp.c', (error, stdout, stderr) => {
  if (error) {
  	console.error(`exec error: ${error}`);
  	return;
  }
  console.log(stdout);
});

Changes to src/radicalc.ne.

25
26
27
28
29
30
31









32
33
34
35
36
37
38
	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 {







>
>
>
>
>
>
>
>
>







25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
	function(d) { return {
		name: d[2],
		params: d[6],
		dataTypeStr: d[10] || "void",
		block: d[12]
	}}
%}

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

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

FunDefParam -> ident _ Type {%
	function(d) { return {

Added src/rcs_std.h.















































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>

typedef int8_t   rcst__i8;
typedef int16_t  rcst__i16;
typedef int32_t  rcst__i32;
typedef int64_t  rcst__i64;
typedef uint8_t  rcst__u8;
typedef uint16_t rcst__u16;
typedef uint32_t rcst__u32;
typedef uint64_t rcst__u64;
typedef void     rcst__void;
typedef bool     rcst__bool;

#define RCST_M(T) \
rcst__##T rcsf__add__##T##__##T##__##T (rcst__##T a, rcst__##T b) {return a+b;}\
rcst__##T rcsf__sub__##T##__##T##__##T (rcst__##T a, rcst__##T b) {return a-b;}\
rcst__##T rcsf__mul__##T##__##T##__##T (rcst__##T a, rcst__##T b) {return a*b;}\
rcst__##T rcsf__div__##T##__##T##__##T (rcst__##T a, rcst__##T b) {return a/b;}\
rcst__bool rcsf__is_less_or_eq__bool__##T##__##T \
(rcst__##T a, rcst__##T b) {return a <= b;} \
rcst__bool rcsf__is_less__bool__##T##__##T \
(rcst__##T a, rcst__##T b) {return a < b;} \
rcst__bool rcsf__is_greater_or_eq__bool__##T##__##T \
(rcst__##T a, rcst__##T b) {return a >= b;} \
rcst__bool rcsf__is_greater__bool__##T##__##T \
(rcst__##T a, rcst__##T b) {return a > b;} \
rcst__bool rcsf__is_eq__bool__##T##__##T \
(rcst__##T a, rcst__##T b) {return a == b;} \
rcst__bool rcsf__is_not_eq__bool__##T##__##T \
(rcst__##T a, rcst__##T b) {return a != b;}

RCST_M(i8)
RCST_M(i16)
RCST_M(i32)
RCST_M(i64)
RCST_M(u8)
RCST_M(u16)
RCST_M(u32)
RCST_M(u64)

#undef RCST_M

rcst__bool rcsf__is_eq__bool__bool__bool(rcst__bool a, rcst__bool b) {
	return a == b;
}

rcst__bool rcsf__is_not_eq__bool__bool__bool(rcst__bool a, rcst__bool b) {
	return a != b;
}

rcst__void rcsf__print__void__i32(rcst__i32 a) {
	printf("%d\n", a);
}

Added src/test.rcs.







































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
fun mul(a:i32, b:i32):i32 stub
fun add(a:i32, b:i32):i32 stub
fun is_less_or_eq(a:i32, b:i32):bool stub
fun print(a:i32) stub


fun factorial(n:i32):i32
	var result = 1
	var i = 1
	while i <= n
		result = result * i
		i = i + 1
	end
	return result
end

fun main()
	print(factorial(12))
end