Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/codegen/codegen.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ class JSCodeGen {
code += this._ind() + '}';
return code;
}
ForStatement(expression) {
let code = expression.body.map((exp) => this._generate(exp)).join('\n');
return code;
}

ReturnStatement({ argument }) {
return `return ${this._generate(argument)};`;
Expand Down
45 changes: 43 additions & 2 deletions src/transpiler/eva-mpp.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ class EvaMPP {
right: this._generate(right),
};
}
// assignment = [++ x]
// assignment = [-- x]
// return = (return 1)
if (expression[0] === 'return') {
const [_tag, args] = expression;
Expand Down Expand Up @@ -159,6 +161,12 @@ class EvaMPP {
case 'not':
operator = '!';
break;
case '--':
operator = '--';
break;
case '++':
operator = '++';
break;
default:
throw `Unknown unary operator: ${expression[0]}`;
}
Expand Down Expand Up @@ -244,6 +252,38 @@ class EvaMPP {
};
}

/*
(for (var index 0) (< index 10) (set index (+ index 1))
(print "current index: " index)
)
*/

if (expression[0] === 'for') {
const [_tag, init, test, update, ForBody] = expression;
let body;
if (update.length === 0) {
body = this._generate(['begin', ForBody]);
} else {
body = this._generate(['begin', ForBody, update]);
}
const whileAst = {
type: types.WhileStatement,
test: this._generate(test),
body: this._toStatement(body),
};
let BodyAst;
if (init.length === 0) {
BodyAst = [whileAst];
} else {
BodyAst = [this._generate(init), whileAst];
}
const BlockAst = {
type: `ForStatement`,
body: BodyAst,
};
return BlockAst;
}

// pattern match

if (expression[0] === 'match') {
Expand Down Expand Up @@ -540,7 +580,8 @@ class EvaMPP {
if (expression.length !== 2) {
return false;
}
return Boolean(expression[0] === 'not' || expression[0] === '-');
const test = expression[0];
return Boolean(test === 'not' || test === '-' || test === '++' || test === '--');
}
_isLogicalBinary(expression) {
const operator = expression[0];
Expand All @@ -557,7 +598,7 @@ class EvaMPP {
saveToFile(filename, code) {
const runtimeCode = `
// prologue
// const {print, spawn, send, receive, sleep, scheduler, NextMath } = require('../src/runtime');
const {print, spawn, send, receive, sleep, scheduler, NextMath } = require('../src/runtime');
${code}
`;
writeFileSync(filename, runtimeCode);
Expand Down
49 changes: 4 additions & 45 deletions tests/out.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,9 @@

// prologue
const {print, spawn, send, receive, sleep, scheduler, NextMath } = require('../src/runtime');

function success(param) {
return print("param = ", param);
}


function notFound(url) {
return print("not-found:", url);
}


async function* _handleConnection(data) {
let request = await receive(this);
try {
if(("hello" !== request)) throw NextMath
success(20);
} catch (e) {
if((e !== NextMath)) throw e
try {
let { code: _code, len: len } = request;
if((_code !== 200)) throw NextMath
success(len);
yield*_handleConnection.call(this, data);
} catch (e) {
if((e !== NextMath)) throw e
try {
let { code: _code, url: url } = request;
if((_code !== 204)) throw NextMath
notFound(url);
} catch (e) {
if((e !== NextMath)) throw e
return print(data);
}
}
}
}

let handler = spawn(_handleConnection, "default");
let index = 0;

function sendMessage() {
index = (index + 1);
if((index <= 5)) send(handler, { code: 200, len: index }); else send(handler, "hello");
}

setInterval(sendMessage, 100);
while((index < 10)) {
print("current index: ", index);
++index
}

8 changes: 7 additions & 1 deletion tests/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,13 @@ const web = `
)
)
`;
const { ast, target } = evaMPP.compile(web);

const for_loop = `
(for (var index 0) (< index 10) (++ index)
(print "current index: " index)
)
`;
const { ast, target } = evaMPP.compile(for_loop, './tests/out.js');

console.log('---- ast ----');
console.log(JSON.stringify(ast, null, 2));
Expand Down