Skip to main content

module lang::flybytes::demo::pico::Compiler

rascal-0.40.17
flybytes-0.2.8

Usage

import lang::flybytes::demo::pico::Compiler;

Source code

http://github.com/usethesource/flybytes/blob/main/src/lang/flybytes/demo/pico/Compiler.rsc

Dependencies

import lang::flybytes::demo::pico::Syntax;
import lang::flybytes::Syntax;
import lang::flybytes::Compiler;
import lang::flybytes::api::System;
import lang::flybytes::api::Object;
import lang::flybytes::api::String;
import lang::flybytes::api::JavaLang;
import lang::flybytes::macros::ControlFlow;
import IO;
import String;
import ParseTree;

function parse

Program parse(loc program) = parse(#start[Program], program).top;

function testFactorial

void testFactorial() {
Program tree = parse(|project://flybytes/src/lang/flybytes/demo/pico/fac.pico|);
println(tree);
compileProgram(tree, "Factorial", |project://flybytes/generated|);
}

function testITE

void testITE() {
Program tree = parse(|project://flybytes/src/lang/flybytes/demo/pico/ite.pico|);
println(tree);
compileProgram(tree, "ITE", |project://flybytes/generated|);
}

function compileProgram

void compileProgram(Program p, str name, loc folder) {
cl = compileProgram(p, name);
compileClass(compileProgram(p, name), folder + "<name>.class", debugMode=true);
}


Class compileProgram(Program p, str name)
= class(object(name),
methods=[
main("$$args", [
*decls(p.decls),
*commandline(p.decls),
*stats(p.body),
*output(p.decls),
\return()
])[src=p@\loc]
]
)[src=p@\loc];

function decls

list[Stat] decls(Declarations p)
= [decl(\type(t), "<i>")[src=i@\loc] | (IdType) `<Id i> : <Type t>` <- p.decls];

function \type

Type \type((Type) `natural`) = integer();

Type \type((Type) `string`) = string();

function stats

list[Stat] stats({Statement  ";"}* stats) = [stat(s)[src=s@\loc] | s <- stats];

function stat

Stat stat(s:(Statement) `<Id var> := <Expression val>`)
= store("<var>", expr(val));

Stat stat(s:(Statement)
`if <Expression cond> then
' <{Statement ";"}* thenPart>
'else
' <{Statement ";"}* elsePart>
'fi`)
= \if(ne(expr(cond), iconst(0)), stats(thenPart), stats(elsePart)) when /Id _ := thenPart;

Stat stat(s:(Statement)
`while <Expression cond> do
' <{Statement ";"}* body>
'od`)
= \while(expr(cond), stats(body));

function expr

Exp expr(e:(Expression) `<Id name>`)                        = load("<name>", src=e@\loc);

Exp expr(e:(Expression) `<String s>`) = const(string(), "<s>"[1..-1], src=e@\loc);

Exp expr(e:(Expression) `<Natural natcon>`) = const(integer(), toInt("<natcon>"), src=e@\loc);

Exp expr(e:(Expression) `(<Expression e>)`) = expr(e);

Exp expr(e:(Expression) `<Expression l> || <Expression r>`) = String_concat(expr(l), expr(r))[src=e@\loc];

Exp expr(e:(Expression) `<Expression l> + <Expression r>`) = add(expr(l), expr(r), src=e@\loc);

Exp expr(e:(Expression) `<Expression l> - <Expression r>`) = sub(expr(l), expr(r), src=e@\loc);

function output

list[Stat] output(Declarations p)
= [stdout(String_concat(const(string(), "<i>\t: "), toString(i, t)))[src=i@\loc]
| (IdType) `<Id i> : <Type t>` <- p.decls]
;

function toString

Exp toString(Id i, (Type) `natural`) 
= invokeStatic(object("java.lang.Integer"), methodDesc(string(), "toString", [integer()]), [load("<i>")])[src=i@\loc];

Exp toString(Id i, (Type) `string`)
= load("<i>", src=i@\loc);

function commandline

list[Stat] commandline(Declarations p) 
= [for_array("$$args", "i", [
// if (args[i].equals(varName))
\if (equals(sconst("<i>"), aload(load("$$args"), load("i"))), [
// varName = fromString(args[i+1])
store("<i>", fromString(t, aload(load("$$args"), add(load("i"), iconst(1)))))
])[src=i@\loc]
])
| (IdType) `<Id i> : <Type t>` <- p.decls];

function fromString

Exp fromString((Type) `natural`, Exp e) = Integer_parseInt(e, 10);

Exp fromString((Type) `string`, Exp e) = e;