"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.astToSExp = void 0;
-const parsec = require("typescript-parsec"); // import parsec
+const p = require("typescript-parsec"); // import p
/* for test */
const assert = require("assert");
/** the type of token */
var TokenKind;
(function (TokenKind) {
- TokenKind[TokenKind["Int"] = 0] = "Int";
- TokenKind[TokenKind["Flo"] = 1] = "Flo";
- TokenKind[TokenKind["Id"] = 2] = "Id";
- TokenKind[TokenKind["At"] = 3] = "At";
- TokenKind[TokenKind["Comt"] = 4] = "Comt";
- TokenKind[TokenKind["Str"] = 5] = "Str";
- TokenKind[TokenKind["Lambda"] = 6] = "Lambda";
- TokenKind[TokenKind["Assign"] = 7] = "Assign";
- TokenKind[TokenKind["Set"] = 8] = "Set";
- TokenKind[TokenKind["Keyword"] = 9] = "Keyword";
- TokenKind[TokenKind["LParen"] = 10] = "LParen";
- TokenKind[TokenKind["RParen"] = 11] = "RParen";
- TokenKind[TokenKind["Space"] = 12] = "Space";
- TokenKind[TokenKind["NewPara"] = 13] = "NewPara";
- TokenKind[TokenKind["MainTxt"] = 14] = "MainTxt";
+ TokenKind[TokenKind["Flo"] = 0] = "Flo";
+ TokenKind[TokenKind["Int"] = 1] = "Int";
+ TokenKind[TokenKind["At"] = 2] = "At";
+ TokenKind[TokenKind["Id"] = 3] = "Id";
+ TokenKind[TokenKind["RArr"] = 4] = "RArr";
+ TokenKind[TokenKind["SColon"] = 5] = "SColon";
+ TokenKind[TokenKind["LPar"] = 6] = "LPar";
+ TokenKind[TokenKind["RPar"] = 7] = "RPar";
+ TokenKind[TokenKind["Assign"] = 8] = "Assign";
+ TokenKind[TokenKind["Op"] = 9] = "Op";
+ TokenKind[TokenKind["Hash"] = 10] = "Hash";
+ TokenKind[TokenKind["Com"] = 11] = "Com";
+ TokenKind[TokenKind["BSlash"] = 12] = "BSlash";
+ TokenKind[TokenKind["Str"] = 13] = "Str";
+ TokenKind[TokenKind["LitStr"] = 14] = "LitStr";
+ TokenKind[TokenKind["Space"] = 15] = "Space";
})(TokenKind || (TokenKind = {}));
/** from AST to S-exp */
function astToSExp(ast) {
}
exports.astToSExp = astToSExp;
// tokenizer
-const tokenizer = parsec.buildLexer([
+const tokenizer = p.buildLexer([
+ [true, /^\d+[.]\d+/g, TokenKind.Flo],
[true, /^\d+/g, TokenKind.Int],
- [true, /^\d+\.\d+/g, TokenKind.Flo],
- [true, /^(let|in|fn)/g, TokenKind.Keyword],
- [true, /^[_a-zA-Z][_0-9a-zA-Z]*/g, TokenKind.Id],
- [true, /^\@/g, TokenKind.At],
- /* inside comment, only accept 1. non / character
- or 2. "/ + non * character" */
- [true, /^\/\*(\/[^*]|[^\\]?)*\*\//g, TokenKind.Comt],
- [true, /^\"(\\\"|[^\"]?)*\"/g, TokenKind.Str],
- [true, /^\:\=/g, TokenKind.Set],
- [true, /^\=/g, TokenKind.Assign],
- [true, /^->/g, TokenKind.Lambda],
- [true, /^\(/g, TokenKind.LParen],
- [true, /^\)/g, TokenKind.RParen],
- [true, /^([ \t]+|[ \t]*\r?\n[ \t]*)/g, TokenKind.Space],
- [true, /^(\r?\n){2}/g, TokenKind.NewPara],
- [true, /^(\\\@|[^@\s])/g, TokenKind.MainTxt],
+ [true, /^[@]/g, TokenKind.At],
+ [true, /^[_\w][_\d\w]*/g, TokenKind.Id],
+ [true, /^->/g, TokenKind.RArr],
+ [true, /^[;]/g, TokenKind.SColon],
+ [true, /^[(]/g, TokenKind.LPar],
+ [true, /^[)]/g, TokenKind.RPar],
+ [true, /^[=]/g, TokenKind.Assign],
+ [true, /^([\+\-\*\/]|[!<>=]=)/g, TokenKind.Op],
+ [true, /^#[^#]*#/g, TokenKind.Com],
+ [true, /^[\\]/g, TokenKind.BSlash],
+ [true, /^\"([^"]|[\\\"])*\"/g, TokenKind.Str],
+ [true, /^([^\\]+?)/g, TokenKind.LitStr],
+ [true, /^\s+/g, TokenKind.Space],
]);
/** ignore spaces ,new lines, and comments */
-const _ = parsec.opt(parsec.alt(parsec.tok(TokenKind.Space), parsec.tok(TokenKind.NewPara),
-// space or newPara + comment + space or newPara
-parsec.seq(parsec.opt(parsec.alt(parsec.tok(TokenKind.Space), parsec.tok(TokenKind.NewPara))), parsec.tok(TokenKind.Comt), parsec.opt(parsec.alt(parsec.tok(TokenKind.Space), parsec.tok(TokenKind.NewPara))))));
+//const _ = p.opt(p.alt(
+// p.tok(TokenKind.Space),
+// p.tok(TokenKind.Com),
+// )
+//);
function applyInteger(value) {
// extend value to ASTNode
const newNode = {
};
return newNode;
}
-function applyIdentifier(value) {
- const newNode = {
- actualValue: value.text,
- ...value
- };
- return newNode;
-}
-/** apply LETTING.
- * returns [let, [var, x], expr] */
-function applyLetting(input) {
- // node representing let
- let letNode = input[0];
- let varNode = input[2];
- let valueNode = input[6];
- let exprAST = input[10];
- return [letNode, [varNode, valueNode], exprAST];
-}
-/** apply SETTING */
-function applySetting(input) {
- // node representing let
- let setNode = input[2];
- let varNode = input[0];
- let valueNode = input[4];
- let exprAST = input[8];
- // (:= (var val) expr) : set var = val in expr
- return [setNode, [varNode, valueNode], exprAST];
-}
-function applyLambda(input) {
- let lambdaNode = input[0];
- let argHead = input[1];
- let argTail = input[2];
- let body = input[6];
- let args = [argHead].concat(argTail);
- // return (fn (args) body) like lambda in Scheme
- return [lambdaNode, args, body];
-}
-function applyApplying(input) {
- let applier = input[0];
- let applieeHead = input[2];
- let applieeTail = input[3];
- let appliee = [(applieeHead)].concat(applieeTail);
- // foo 2 3 => (foo (2 3))
- return [applier, appliee];
-}
/** define all the parser sentence */
-const CONST = parsec.rule();
-const VAR = parsec.rule();
-const ARG = parsec.rule();
-const EXPR = parsec.rule();
-const LETTING = parsec.rule();
-const SETTING = parsec.rule();
-const LAMBDA = parsec.rule();
-const APPLYING = parsec.rule();
-/*
-CONST ::= INT | FLOAT | STRING
-*/
-CONST.setPattern(parsec.alt(parsec.apply(parsec.tok(TokenKind.Int), applyInteger), parsec.apply(parsec.tok(TokenKind.Flo), applyFloat), parsec.apply(parsec.tok(TokenKind.Str), applyString)));
-/** VAR = ID */
-VAR.setPattern(parsec.apply(parsec.tok(TokenKind.Id), applyIdentifier));
-/** ARG = ID */
-ARG.setPattern(parsec.apply(parsec.tok(TokenKind.Id), applyIdentifier));
-/**SETTING ::= VAR ":=" EXPR in EXPR
- * and ignore the spaces and new lines with `_`
-*/
-SETTING.setPattern(parsec.apply(parsec.seq(VAR, _, parsec.str(":="), _, EXPR, _, parsec.str("in"), _, EXPR), applySetting));
-/**LETTING ::= "let" VAR "=" EXPR in EXPR
- * and ignore the spaces and new lines with `_`
-*/
-LETTING.setPattern(parsec.apply(parsec.seq(parsec.str("let"), _, VAR, _, parsec.str("="), _, EXPR, _, parsec.str("in"), _, EXPR), applyLetting));
-/**LAMBDA ::= "fn" (Args)+ "->" EXPR
- * and ignore the spaces and new lines with `_`
-*/
-LAMBDA.setPattern(parsec.apply(parsec.seq(parsec.str("fn"), parsec.kright(_, ARG), // arg SpaceNL
-parsec.rep_sc(parsec.kright(_, ARG)), //other (arg SpaceNL), repeat 0+times
-_, parsec.str("->"), _, EXPR), applyLambda));
-// APPLYING = ( "(" APPLYING ")" |LAMBDA|VAR) APPLIEE+
-APPLYING.setPattern(parsec.apply(parsec.seq(parsec.alt(LAMBDA, VAR, parsec.kmid(parsec.seq(parsec.str('('), _), APPLYING, parsec.seq(_, parsec.str(')')))), _, EXPR, parsec.rep_sc(parsec.kright(_, EXPR))), applyApplying));
-/** EXPR = CONST | VAR
- * | LETTING | SETTING
- * | LAMBDA | APPLYING
- * | "(" APPLYING ")" */
-EXPR.setPattern(parsec.alt(CONST, VAR, LETTING, SETTING, LAMBDA, parsec.kmid(parsec.seq(parsec.str('('), _), APPLYING, parsec.seq(_, parsec.str(')')))));
+const CONST = p.rule();
+/*const VAR = p.rule<TokenKind, ASTNode>();
+const ARG = p.rule<TokenKind, ASTNode>();
+const EXPR = p.rule<TokenKind, AST>();
+const LETTING = p.rule<TokenKind, AST>();
+const LAMBDA = p.rule<TokenKind, AST>();
+const APPLYING = p.rule<TokenKind, AST>(); */
+CONST.setPattern(p.alt(p.apply(p.tok(TokenKind.Flo), applyFloat), p.apply(p.tok(TokenKind.Int), applyInteger), p.apply(p.tok(TokenKind.Str), applyString)));
function mainParse(inputStr) {
- return parsec.expectSingleResult(parsec.expectEOF(EXPR.parse(tokenizer.parse(inputStr))));
+ return p.expectSingleResult(p.expectEOF(CONST.parse(tokenizer.parse(inputStr))));
}
// test
function main() {
- // bigint has suffix `n`
- assert.strictEqual(mainParse('123455667').actualValue, 123455667n);
- assert.strictEqual(mainParse('000').actualValue, 0n);
- assert.strictEqual(mainParse('1.22').actualValue, 1.22);
- assert.strictEqual(mainParse('0.0').actualValue, 0.0);
- assert.strictEqual(mainParse(`""`).actualValue, "");
- assert.strictEqual(mainParse(`"the little town"`).actualValue, `the little town`);
- assert.strictEqual(mainParse(`"\\\"Alice\\\""`).actualValue, `"Alice"`);
- assert.strictEqual(mainParse(`foo`).actualValue, "foo");
- assert.strictEqual(astToSExp(mainParse(`let x = 12 in 23`)), "(let (x 12) 23)");
- assert.strictEqual(astToSExp(mainParse(`let y = 10 in let x = 12 in 23`)), "(let (y 10) (let (x 12) 23))");
- assert.strictEqual(astToSExp(mainParse(`let y = 10 in y := 12 in 23`)), "(let (y 10) (:= (y 12) 23))");
- assert.strictEqual(astToSExp(mainParse(`fn x y -> 234`)), "(fn (x y) 234)");
- assert.strictEqual(astToSExp(mainParse(`(add 12 23 )`)), "(add (12 23))");
- assert.strictEqual(astToSExp(mainParse(`(foo x y)`)), "(foo (x y))");
- assert.strictEqual(astToSExp(mainParse(`((foo 6 7) bar)`)), "((foo (6 7)) (bar))");
- assert.strictEqual(astToSExp(mainParse(`fn x y ->
- /* foo bar */
- (foo x y)`)), "(fn (x y) (foo (x y)))");
+ assert.strictEqual(mainParse("123").actualValue, 123n);
+ assert.strictEqual(mainParse("3.14").actualValue, 3.14);
+ assert.strictEqual(mainParse("\"foo\"").actualValue, "foo");
}
;
main();
-{"version":3,"file":"index.js","sour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6CAK6C;AAE7C,KAAK,CAAC,UAAU,CACZ,CAAC,CAAC,GAAG,CACD,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,UAAU,CAAC,EACzC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,YAAY,CAAC,EAC3C,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,WAAW,CAAC,CAE7C,CACJ,CAAC;AAGF,SAAS,SAAS,CAAC,QAAiB;IACpC,OAAO,CAAC,CAAC,kBAAkB,CAAC,CAAC,CAAC,SAAS,CACnC,KAAK,CAAC,KAAK,CAAC,SAAS,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC;AAC7C,CAAC;AAGD,OAAO;AACP,SAAS,IAAI;IACT,MAAM,CAAC,WAAW,CAAS,SAAS,CAAC,KAAK,CAAC,CAAC,WAAW,EAAE,IAAI,CAAC,CAAC;IAC/D,MAAM,CAAC,WAAW,CAAS,SAAS,CAAC,MAAM,CAAC,CAAC,WAAW,EAAE,IAAI,CAAC,CAAC;IAChE,MAAM,CAAC,WAAW,CAAS,SAAS,CAAC,SAAS,CAAC,CAAC,WAAW,EAAE,KAAK,CAAC,CAAC;AAGxE,CAAC;AAAA,CAAC;AAEF,IAAI,EAAE,CAAC"}
\ No newline at end of file
-import * as parsec from 'typescript-parsec'; // import parsec
+import * as p from 'typescript-parsec'; // import p
/* for test */
import * as assert from 'assert';
/** the type of token */
enum TokenKind {
- Int, // 3
- Flo, // 3.1416
- Id, // foo, _123, etc
+ Flo,
+ Int,
At, // @
- Comt, // comment /*
- Str, /** "foo" */
- Lambda, /** -> */
- Assign, /** = */
- Set, /** := */
- Keyword, /** let, in */
- LParen, /** ( */
- RParen, /** ) */
- Space, /** semi-width space tab, \r\n? */
- NewPara, /** breaking paragraph, (\r\n?){2} */
- MainTxt, /** used in main text */
+ Id, // identifier
+ RArr, // Right Arrow
+ SColon, // Semi colon
+ LPar, // left perenthesis
+ RPar, // right paranthesis
+ Assign, // =
+ Op, // +-*/...
+ Hash, // #
+ Com, // # comment
+ BSlash, // backslash\
+ Str, // "string"
+ LitStr, // literal string
+ Space, // Spaces
}
// add "actualValue" in the parsed Token
-export interface ASTNode extends parsec.Token<TokenKind>{
+export interface ASTNode extends p.Token<TokenKind>{
// number is for float number;
//it's optional. since keyword has no value
actualValue? : bigint | number | string;
// tokenizer
-const tokenizer = parsec.buildLexer([
+const tokenizer = p.buildLexer([
+ [true,/^\d+[.]\d+/g , TokenKind.Flo],
[true, /^\d+/g, TokenKind.Int],
- [true, /^\d+\.\d+/g, TokenKind.Flo],
- [true, /^(let|in|fn)/g, TokenKind.Keyword], // let, in, fn
- [true, /^[_a-zA-Z][_0-9a-zA-Z]*/g, TokenKind.Id],
- [true, /^\@/g, TokenKind.At],
- /* inside comment, only accept 1. non / character
- or 2. "/ + non * character" */
- [true, /^\/\*(\/[^*]|[^\\]?)*\*\//g, TokenKind.Comt],
- [true, /^\"(\\\"|[^\"]?)*\"/g, TokenKind.Str],
- [true, /^\:\=/g, TokenKind.Set],
- [true, /^\=/g, TokenKind.Assign],
- [true, /^->/g, TokenKind.Lambda],
- [true, /^\(/g, TokenKind.LParen],
- [true, /^\)/g, TokenKind.RParen],
- [true, /^([ \t]+|[ \t]*\r?\n[ \t]*)/g, TokenKind.Space],
- [true, /^(\r?\n){2}/g, TokenKind.NewPara],
- [true, /^(\\\@|[^@\s])/g, TokenKind.MainTxt],
+ [true,/^[@]/g, TokenKind.At ],
+ [true,/^[_\w][_\d\w]*/g, TokenKind.Id ],
+ [true,/^->/g , TokenKind.RArr ],
+ [true, /^[;]/g, TokenKind.SColon ],
+ [true, /^[(]/g, TokenKind.LPar ],
+ [true, /^[)]/g, TokenKind.RPar ],
+ [true, /^[=]/g, TokenKind.Assign ],
+ [true, /^([\+\-\*\/]|[!<>=]=)/g, TokenKind.Op ],
+ [true, /^#[^#]*#/g, TokenKind.Com ],
+ [true, /^[\\]/g, TokenKind.BSlash ],
+ [true,/^\"([^"]|[\\\"])*\"/g , TokenKind.Str ],
+ [true, /^([^\\]+?)/g, TokenKind.LitStr ],
+ [true, /^\s+/g, TokenKind.Space],
]);
-/** ignore spaces ,new lines, and comments */
-const _ = parsec.opt(parsec.alt(
- parsec.tok(TokenKind.Space),
- parsec.tok(TokenKind.NewPara),
-
- // space or newPara + comment + space or newPara
- parsec.seq(
- parsec.opt(parsec.alt(
- parsec.tok(TokenKind.Space),
- parsec.tok(TokenKind.NewPara))),
- parsec.tok(TokenKind.Comt),
- parsec.opt(parsec.alt(
- parsec.tok(TokenKind.Space),
- parsec.tok(TokenKind.NewPara))),
- )
- )
-);
+/** ignore spaces ,new lines, and comments */
+//const _ = p.opt(p.alt(
+// p.tok(TokenKind.Space),
+// p.tok(TokenKind.Com),
+// )
+//);
-function applyInteger(value: parsec.Token<TokenKind.Int>): ASTNode {
+function applyInteger(value: p.Token<TokenKind.Int>): ASTNode {
// extend value to ASTNode
const newNode : ASTNode = {
actualValue : BigInt(value.text) ,
return newNode;
}
-function applyFloat(value: parsec.Token<TokenKind.Flo>): ASTNode {
+function applyFloat(value: p.Token<TokenKind.Flo>): ASTNode {
const newNode : ASTNode = {
actualValue : parseFloat(value.text) ,
...value};
return newNode;
}
-function applyString(value: parsec.Token<TokenKind.Str>): ASTNode {
+function applyString(value: p.Token<TokenKind.Str>): ASTNode {
const newNode : ASTNode = {
// get only text[1,2,...,the second last char]
actualValue : value.text.slice(1,value.text.length-1).replace(/\\\"/g, "\"") ,
return newNode;
}
-function applyIdentifier(value: parsec.Token<TokenKind.Id>): ASTNode {
- const newNode : ASTNode = {
- actualValue : value.text,
- ...value};
- return newNode;
-}
-
-/** apply LETTING.
- * returns [let, [var, x], expr] */
-function applyLetting(input: [parsec.Token<TokenKind>, // let
- parsec.Token<TokenKind> | undefined, // space
- ASTNode, // var
- parsec.Token<TokenKind>| undefined, // space
- parsec.Token<TokenKind>, // =
- parsec.Token<TokenKind>| undefined, // space
- AST, // val
- parsec.Token<TokenKind>| undefined, // space
- parsec.Token<TokenKind>, // in
- parsec.Token<TokenKind>| undefined, // space
- AST // expr
- ]): AST {
- // node representing let
- let letNode : ASTNode = input[0];
- let varNode = input[2];
- let valueNode = input[6];
- let exprAST = input[10];
-
- return [letNode, [varNode, valueNode], exprAST];
-
-}
-
-/** apply SETTING */
-function applySetting(input: [ASTNode, // var
- parsec.Token<TokenKind>| undefined, // space
- parsec.Token<TokenKind>, // :=
- parsec.Token<TokenKind>| undefined, // space
- AST, // val
- parsec.Token<TokenKind>| undefined, // space
- parsec.Token<TokenKind>, // in
- parsec.Token<TokenKind>| undefined, // space
- AST // expr
- ]): AST {
- // node representing let
- let setNode = input[2];
- let varNode = input[0];
- let valueNode = input[4];
- let exprAST = input[8];
-
- // (:= (var val) expr) : set var = val in expr
- return [setNode, [varNode, valueNode], exprAST];
-
-}
-
-function applyLambda(input: [ASTNode, // fn
- ASTNode, // arg
- ASTNode[], // args
- parsec.Token<TokenKind>| undefined, // space
- parsec.Token<TokenKind>, // ->
- parsec.Token<TokenKind>| undefined, // space
- AST // expr
-]): AST {
- let lambdaNode = input[0];
- let argHead = input[1];
- let argTail = input[2];
- let body = input[6];
- let args = [argHead].concat(argTail)
-
- // return (fn (args) body) like lambda in Scheme
- return [lambdaNode, args, body];
-}
-
-
-function applyApplying(input : [ASTNode, // caller
- parsec.Token<TokenKind> |undefined, // space
- ASTNode, // head of callee
- AST[] // tail of callee
- ]){
- let applier = input[0];
- let applieeHead = input[2];
- let applieeTail = input[3];
- let appliee = [<AST>(applieeHead)].concat(applieeTail);
-
-
- // foo 2 3 => (foo (2 3))
- return [applier, appliee];
-
-}
-
/** define all the parser sentence */
-const CONST = parsec.rule<TokenKind, ASTNode>();
-const VAR = parsec.rule<TokenKind, ASTNode>();
-const ARG = parsec.rule<TokenKind, ASTNode>();
-const EXPR = parsec.rule<TokenKind, AST>();
-const LETTING = parsec.rule<TokenKind, AST>();
-const SETTING = parsec.rule<TokenKind, AST>();
-const LAMBDA = parsec.rule<TokenKind, AST>();
-const APPLYING = parsec.rule<TokenKind, AST>();
-
+const CONST = p.rule<TokenKind, ASTNode>();
+/*const VAR = p.rule<TokenKind, ASTNode>();
+const ARG = p.rule<TokenKind, ASTNode>();
+const EXPR = p.rule<TokenKind, AST>();
+const LETTING = p.rule<TokenKind, AST>();
+const LAMBDA = p.rule<TokenKind, AST>();
+const APPLYING = p.rule<TokenKind, AST>(); */
-
-/*
-CONST ::= INT | FLOAT | STRING
-*/
CONST.setPattern(
- parsec.alt(
- parsec.apply(parsec.tok(TokenKind.Int), applyInteger),
- parsec.apply(parsec.tok(TokenKind.Flo), applyFloat),
- parsec.apply(parsec.tok(TokenKind.Str), applyString),
- )
-);
-
-/** VAR = ID */
-VAR.setPattern(
- parsec.apply(parsec.tok(TokenKind.Id), applyIdentifier),
-);
-
-/** ARG = ID */
-ARG.setPattern(
- parsec.apply(parsec.tok(TokenKind.Id), applyIdentifier),
-);
-
-
+ p.alt(
+ p.apply(p.tok(TokenKind.Flo), applyFloat),
+ p.apply(p.tok(TokenKind.Int), applyInteger),
+ p.apply(p.tok(TokenKind.Str), applyString),
-
-/**SETTING ::= VAR ":=" EXPR in EXPR
- * and ignore the spaces and new lines with `_`
-*/
-
-SETTING.setPattern(
- parsec.apply(
- parsec.seq(
- VAR,
- _,
- parsec.str(":="),
- _,
- EXPR,
- _,
- parsec.str("in"),
- _,
- EXPR), applySetting));
-
-/**LETTING ::= "let" VAR "=" EXPR in EXPR
- * and ignore the spaces and new lines with `_`
-*/
-LETTING.setPattern(
- parsec.apply(
- parsec.seq(
- parsec.str("let"),
- _,
- VAR,
- _,
- parsec.str("="),
- _,
- EXPR,
- _,
- parsec.str("in"),
- _,
- EXPR), applyLetting));
-
-
-
-/**LAMBDA ::= "fn" (Args)+ "->" EXPR
- * and ignore the spaces and new lines with `_`
-*/
-LAMBDA.setPattern(
- parsec.apply(
- parsec.seq(
- parsec.str("fn"),
- parsec.kright(_, ARG), // arg SpaceNL
- parsec.rep_sc(parsec.kright(_, ARG)), //other (arg SpaceNL), repeat 0+times
- _,
- parsec.str("->"),
- _,
- EXPR),
- applyLambda)
-)
-
-// APPLYING = ( "(" APPLYING ")" |LAMBDA|VAR) APPLIEE+
-APPLYING.setPattern(
- parsec.apply(
- parsec.seq(
- parsec.alt(
- LAMBDA,
- VAR,
- parsec.kmid(
- parsec.seq(parsec.str('('), _),
- APPLYING,
- parsec.seq(_, parsec.str(')')))
- ),
- _,
- EXPR,
- parsec.rep_sc(parsec.kright(_, EXPR))),
- applyApplying));
-
-/** EXPR = CONST | VAR
- * | LETTING | SETTING
- * | LAMBDA | APPLYING
- * | "(" APPLYING ")" */
-EXPR.setPattern(
- parsec.alt(
- CONST,
- VAR,
- LETTING,
- SETTING,
- LAMBDA,
- parsec.kmid(
- parsec.seq(parsec.str('('), _),
- APPLYING,
- parsec.seq(_, parsec.str(')')))
)
);
-
-
-
function mainParse(inputStr : string){
- return parsec.expectSingleResult(parsec.expectEOF(
- EXPR.parse(tokenizer.parse(inputStr))));
+return p.expectSingleResult(p.expectEOF(
+ CONST.parse(tokenizer.parse(inputStr))));
}
// test
function main(){
- // bigint has suffix `n`
- assert.strictEqual((<ASTNode>mainParse('123455667')).actualValue, 123455667n);
- assert.strictEqual((<ASTNode>mainParse('000')).actualValue, 0n);
- assert.strictEqual((<ASTNode>mainParse('1.22')).actualValue, 1.22);
- assert.strictEqual((<ASTNode>mainParse('0.0')).actualValue, 0.0);
- assert.strictEqual((<ASTNode>mainParse(`""`)).actualValue, "");
- assert.strictEqual((<ASTNode>mainParse(`"the little town"`)).actualValue, `the little town`);
- assert.strictEqual((<ASTNode>mainParse(`"\\\"Alice\\\""`)).actualValue, `"Alice"`);
-
- assert.strictEqual((<ASTNode>mainParse(`foo`)).actualValue, "foo");
- assert.strictEqual(astToSExp(mainParse(`let x = 12 in 23`)), "(let (x 12) 23)");
- assert.strictEqual(astToSExp(mainParse(`let y = 10 in let x = 12 in 23`)), "(let (y 10) (let (x 12) 23))");
- assert.strictEqual(astToSExp(mainParse(`let y = 10 in y := 12 in 23`)), "(let (y 10) (:= (y 12) 23))");
- assert.strictEqual(astToSExp(mainParse(`fn x y -> 234`)), "(fn (x y) 234)");
- assert.strictEqual(astToSExp(mainParse(`(add 12 23 )`)), "(add (12 23))");
- assert.strictEqual(astToSExp(mainParse(`(foo x y)`)), "(foo (x y))");
- assert.strictEqual(astToSExp(mainParse(`((foo 6 7) bar)`)), "((foo (6 7)) (bar))");
- assert.strictEqual(astToSExp(mainParse(`fn x y ->
- /* foo bar */
- (foo x y)`)), "(fn (x y) (foo (x y)))");
+ assert.strictEqual(<BigInt>mainParse("123").actualValue, 123n);
+ assert.strictEqual(<BigInt>mainParse("3.14").actualValue, 3.14);
+ assert.strictEqual(<BigInt>mainParse("\"foo\"").actualValue, "foo");
};