Browse Source

Merge pull request #2 from oftn/master

merge OFTN version
pull/21/head
Wesley Kerfoot 11 years ago
parent
commit
7619ec3a0a
  1. 26
      parse.js
  2. 3
      representation.js
  3. 16
      tokenize.js

26
parse.js

@ -1,5 +1,4 @@
#! /usr/bin/node #! /usr/bin/node
var fs = require("fs"); var fs = require("fs");
var typ = require("./representation.js"); var typ = require("./representation.js");
var $ = require("./tools.js"); var $ = require("./tools.js");
@ -401,25 +400,34 @@ function parseIf(tokens) {
} }
else { else {
var ifC = parse(tokens); var ifC = parse(tokens);
if (!fst(tokens) || fst(tokens)[0] !== "thenexp") if (!fst(tokens) || fst(tokens)[0] !== "thenexp") {
throw error.JSyntaxError(fst(tokens)[3], throw error.JSyntaxError(fst(tokens)[3],
fst(tokens)[2], fst(tokens)[2],
"if ``exp'' must be folowed by ``then'' exp, not "+snd(tokens)[0]); "if ``exp'' must be folowed by ``then'' exp, not "+snd(tokens)[0]);
}
else { else {
tokens.pop(); tokens.pop();
var thenC = parse(tokens); var thenC = parse(tokens);
if (fst(tokens) && fst(tokens)[0] === "elsexp") { if (fst(tokens) && fst(tokens)[0] === "elsexp") {
tokens.pop(); tokens.pop();
if (_.size(tokens) < 1) {
throw error.JSyntaxError(linenum,
charnum,
"Unexpected end of source");
}
else {
var elseC = parse(tokens); var elseC = parse(tokens);
return new typ.If(ifC, thenC, elseC); return new typ.If(ifC, thenC, elseC);
}
} }
else { else {
return new typ.If(ifC, thenC); throw error.JSyntaxError(linenum,
} charnum,
} "If expression must include an else variant");
} }
}
}
} }

3
representation.js

@ -157,8 +157,7 @@ function DefFunc(ident, params, body) {
function If(condition, thenexp, elseexp) { function If(condition, thenexp, elseexp) {
this.condition = condition; this.condition = condition;
this.thenexp = thenexp; this.thenexp = thenexp;
if (elseexp) this.elseexp = elseexp;
this.elseexp = elseexp;
this.exprType = "If"; this.exprType = "If";
return this; return this;
} }

16
tokenize.js

@ -215,7 +215,7 @@ function tokenize(tokstream, matchop) {
case 45: // '-' case 45: // '-'
lambda = peek(tokstream, "arrow", "->"); lambda = peek(tokstream, "arrow", "->");
if (lambda) { if (lambda) {
tokens.push(lambda); tokens.push($.extend(lambda, [charnum, linenum]));
tokstream = tokstream.substr(2); tokstream = tokstream.substr(2);
break; break;
} }
@ -242,7 +242,7 @@ function tokenize(tokstream, matchop) {
case 116: // 't' case 116: // 't'
result = tokenizeT(tokstream); result = tokenizeT(tokstream);
if (result) { if (result) {
tokens.push(result); tokens.push($.extend(result, [charnum, linenum]));
tokstream = tokstream.substr(4); // 4 = length of either token tokstream = tokstream.substr(4); // 4 = length of either token
break; break;
} }
@ -250,13 +250,13 @@ function tokenize(tokstream, matchop) {
case 105: // 'i' case 105: // 'i'
var ifexp = peek(tokstream, "ifexp", "if"); var ifexp = peek(tokstream, "ifexp", "if");
if (ifexp) { if (ifexp) {
tokens.push(ifexp); tokens.push($.extend(ifexp, [linenum, charnum]));
tokstream = tokstream.substr(2); tokstream = tokstream.substr(2);
break; break;
} }
var inkeyword = peek(tokstream, "in", "in "); var inkeyword = peek(tokstream, "in", "in ");
if (inkeyword) { if (inkeyword) {
tokens.push(inkeyword); tokens.push($.extend(inkeyword, [charnum, linenum]));
tokstream = tokstream.substr(3); tokstream = tokstream.substr(3);
break; break;
} }
@ -279,7 +279,7 @@ function tokenize(tokstream, matchop) {
case 101: // e case 101: // e
result = peek(tokstream, "elsexp", "else"); result = peek(tokstream, "elsexp", "else");
if (result) { if (result) {
tokens.push(result); tokens.push($.extend(result, [charnum, linenum]));
tokstream = tokstream.substr(4); tokstream = tokstream.substr(4);
break; break;
} }
@ -287,7 +287,7 @@ function tokenize(tokstream, matchop) {
case 102: // f case 102: // f
result = peek(tokstream, "falselit", "false"); result = peek(tokstream, "falselit", "false");
if (result) { if (result) {
tokens.push(result); tokens.push($.extend(result, [charnum, linenum]));
tokstream = tokstream.substr(5); tokstream = tokstream.substr(5);
break; break;
} }
@ -295,13 +295,13 @@ function tokenize(tokstream, matchop) {
case 108: // l case 108: // l
lambda = peek(tokstream, "lambda", "lambda"); lambda = peek(tokstream, "lambda", "lambda");
if (lambda) { if (lambda) {
tokens.push(lambda); tokens.push($.extend(lambda, [linenum, charnum]));
tokstream = tokstream.substr(6); tokstream = tokstream.substr(6);
break; break;
} }
var letexp = peek(tokstream, "let", "let"); var letexp = peek(tokstream, "let", "let");
if (letexp) { if (letexp) {
tokens.push(letexp); tokens.push($.extend(letexp, [charnum, linenum]));
tokstream = tokstream.substr(3); tokstream = tokstream.substr(3);
break; break;
} }

Loading…
Cancel
Save