Radicalc  Check-in [e8c0955585]

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

Overview
Comment:linking external functions
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:e8c0955585f6311ad8b8a2b22d86c6424cfd4469b15ff561be2afbfa7388aecf
User & Date: athaudia 2017-08-01 16:47:42
Context
2017-08-02
11:40
changed how returns work check-in: c16abe7c58 user: athaudia tags: trunk
2017-08-01
16:47
linking external functions check-in: e8c0955585 user: athaudia tags: trunk
16:21
intrinsic stubs in seperate file check-in: bbec39aeac user: athaudia tags: trunk
Changes

Changes to src/llvm.js.

8
9
10
11
12
13
14




15
16
17
18
19
20
21

22
23
24
25
26
27
28
...
273
274
275
276
277
278
279

















280
281
282
283
284
285


286
287
288
289
290
291

function outDataType(out, dataType) {
	out.write(dataTypeStr(dataType));
}

function outFunName(out, fun) {
	out.write("@");




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

}

// 1 and not 0 because 0 parses as false
var llvmVar = 1;
var llvmTmp = 1;
var llvmLabel = 1;

................................................................................
		out.write(" ");
		out.write(varStr(p));
	} 
	out.write(") {\n");
	outBlock(out, fun.block);
	out.write("}\n\n");
}


















function outFunctions(out, funs) {
	funs.forEach(function(fun){
		fun.label = newLabel();
	});
	funs.forEach(function(fun){


		if(!fun.isStub)
			outFunction(out, fun);
	});
}

module.exports = {outFunctions: outFunctions, initIntrinsics: initIntrinsics};







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







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>






>
>






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
...
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
305
306
307
308
309
310
311
312
313
314
315

function outDataType(out, dataType) {
	out.write(dataTypeStr(dataType));
}

function outFunName(out, fun) {
	out.write("@");
	if(fun.external) {
		out.write(fun.external);
	}
	else {
		out.write(fun.name);
		out.write("__");
		out.write(fun.dataType.name);
		fun.params.forEach(function(param){
			out.write("__");
			out.write(param.dataType.name);
		});
	}
}

// 1 and not 0 because 0 parses as false
var llvmVar = 1;
var llvmTmp = 1;
var llvmLabel = 1;

................................................................................
		out.write(" ");
		out.write(varStr(p));
	} 
	out.write(") {\n");
	outBlock(out, fun.block);
	out.write("}\n\n");
}

function outExternalFunction(out, fun) {
	out.write("declare ");
	outDataType(out, fun.dataType);
	out.write(" ");
	outFunName(out, fun);
	out.write("(");
	for(var i = 0; i < fun.params.length; ++i) {
		var p = fun.params[i];
		if(i > 0)
			out.write(", ");
		outDataType(out, p.dataType);
		out.write(" ");
		out.write(varStr(p));
	}
	out.write(")");
}

function outFunctions(out, funs) {
	funs.forEach(function(fun){
		fun.label = newLabel();
	});
	funs.forEach(function(fun){
		if(fun.external)
			outExternalFunction(out, fun)
		if(!fun.isStub)
			outFunction(out, fun);
	});
}

module.exports = {outFunctions: outFunctions, initIntrinsics: initIntrinsics};

Changes to src/radicalc.js.

8
9
10
11
12
13
14
15

16
17
18
19
20
21
22
...
216
217
218
219
220
221
222
223
224
225
	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(fs.readFileSync("intrinsics.rcs", "utf8"));

p.feed(src);

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

/* todo:
 - unsigned intrinsics
 - figure out how to link against standard functions
 - strings?
*/







|
>







 







|


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
...
217
218
219
220
221
222
223
224
225
226
	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(fs.readFileSync("intrinsics.rcs", "utf8"))
p.feed(fs.readFileSync("win32.rcs", "utf8"));
p.feed(src);

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

/* todo:
 - unsigned intrinsics
 - how to handle lack of returns?
 - strings?
*/

Changes to src/radicalc.ne.

26
27
28
29
30
31
32
33

34
35
36
37
38

39
40
41
42
43
44
45
		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]}))}
%}








|
>




|
>







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

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

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

Changes to src/test.rcs.

6
7
8
9
10
11
12



13
14
		i = i + 1
	end
	return result
end

fun main():i32
	var i = 0



	return factorial(3*3)
end







>
>
>


6
7
8
9
10
11
12
13
14
15
16
17
		i = i + 1
	end
	return result
end

fun main():i32
	var i = 0
	putch(40)
	putch(41)
	putch(10)
	return factorial(3*3)
end

Added src/win32.rcs.



>
1
fun putch(c:i32):i32 stub "_putch"