fix something
This commit is contained in:
parent
950c4e0423
commit
9d32899ca9
4 changed files with 99 additions and 28 deletions
|
@ -92,10 +92,10 @@ EXPR ::= APPLY | FN | LIST | CONST | VAR | "(" EXPR ")"
|
|||
APPLY ::= OP EXPR+ | EXPR EXPR+
|
||||
FN ::= "fn" ARGS R_ARR BODY
|
||||
ARGS ::= ARG | ARG ARGS
|
||||
ARG ::= "(" TYPE VAR ")"
|
||||
CONST ::= FLO | STR | INT
|
||||
VAR ::= ID
|
||||
TYPE ::= ID
|
||||
* ARG ::= "(" TYPE VAR ")"
|
||||
* CONST ::= FLO | STR | INT
|
||||
* VAR ::= ID
|
||||
* TYPE ::= ID
|
||||
LIST ::= [LIST_INNER]
|
||||
LIST_INNER ::= EXPR | EXPR SEMICOLON LIST_INNER
|
||||
```
|
||||
|
|
|
@ -41,25 +41,34 @@ const tokenizer = p.buildLexer([
|
|||
[true, /^\d+[.]\d+/g, TokenKind.Flo],
|
||||
[true, /^\d+/g, TokenKind.Int],
|
||||
[true, /^[@]/g, TokenKind.At],
|
||||
[true, /^[_\w][_\d\w]*/g, TokenKind.Id],
|
||||
[true, /^[_a-zA-Z][_0-9a-zA-Z]*/g, TokenKind.Id],
|
||||
[true, /^->/g, TokenKind.RArr],
|
||||
[true, /^[;]/g, TokenKind.SColon],
|
||||
[true, /^[(]/g, TokenKind.LPar],
|
||||
[true, /^[)]/g, TokenKind.RPar],
|
||||
[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],
|
||||
[true, /^([^\\]+?)/g, TokenKind.LitStr],
|
||||
]);
|
||||
/** ignore spaces ,new lines, and comments */
|
||||
//const _ = p.opt(p.alt(
|
||||
// p.tok(TokenKind.Space),
|
||||
// p.tok(TokenKind.Com),
|
||||
// )
|
||||
//);
|
||||
const _ = p.opt(p.alt(p.tok(TokenKind.Space), p.tok(TokenKind.Com)));
|
||||
function applyArg(value) {
|
||||
let type = value[2];
|
||||
let variable = value[4];
|
||||
return [type, variable];
|
||||
}
|
||||
function applyID(value) {
|
||||
// extend value to ASTNode
|
||||
const newNode = {
|
||||
actualValue: value.text,
|
||||
...value
|
||||
};
|
||||
return newNode;
|
||||
}
|
||||
function applyInteger(value) {
|
||||
// extend value to ASTNode
|
||||
const newNode = {
|
||||
|
@ -85,21 +94,33 @@ function applyString(value) {
|
|||
}
|
||||
/** define all the parser sentence */
|
||||
const CONST = p.rule();
|
||||
/*const VAR = p.rule<TokenKind, ASTNode>();
|
||||
const ARG = p.rule<TokenKind, ASTNode>();
|
||||
const VAR = p.rule();
|
||||
const TYPE = p.rule();
|
||||
const ARG = p.rule();
|
||||
/*
|
||||
const EXPR = p.rule<TokenKind, AST>();
|
||||
const LETTING = p.rule<TokenKind, AST>();
|
||||
const LAMBDA = p.rule<TokenKind, AST>();
|
||||
const APPLYING = p.rule<TokenKind, AST>(); */
|
||||
/** ARG ::= "(" TYPE VAR ")" */
|
||||
ARG.setPattern(p.apply(p.seq(p.tok(TokenKind.LPar), _, TYPE, _, VAR, _, p.tok(TokenKind.RPar)), applyArg));
|
||||
/** VAR ::= ID */
|
||||
VAR.setPattern(p.apply(p.tok(TokenKind.Id), applyID));
|
||||
/** TYPE ::= ID */
|
||||
TYPE.setPattern(p.apply(p.tok(TokenKind.Id), applyID));
|
||||
/** * CONST ::= FLO | STR | INT */
|
||||
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)));
|
||||
// parse the strings
|
||||
function mainParse(inputStr) {
|
||||
return p.expectSingleResult(p.expectEOF(CONST.parse(tokenizer.parse(inputStr))));
|
||||
// ARG.parse(tokenizer.parse(inputStr))));
|
||||
}
|
||||
// test
|
||||
function main() {
|
||||
assert.strictEqual(mainParse("123").actualValue, 123n);
|
||||
assert.strictEqual(mainParse("3.14").actualValue, 3.14);
|
||||
assert.strictEqual(mainParse("\"foo\"").actualValue, "foo");
|
||||
//assert.strictEqual(astToSExp(mainParse("( int a )")), "(int a)");
|
||||
}
|
||||
;
|
||||
main();
|
||||
|
|
|
@ -1 +1 @@
|
|||
{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;;AAAA,uCAAuC,CAAC,WAAW;AACnD,cAAc;AACd,iCAAiC;AAEjC,yBAAyB;AACzB,IAAK,SAiBJ;AAjBD,WAAK,SAAS;IACV,uCAAG,CAAA;IACH,uCAAG,CAAA;IACH,qCAAE,CAAA;IACF,qCAAE,CAAA;IACF,yCAAI,CAAA;IACJ,6CAAM,CAAA;IACN,yCAAI,CAAA;IACJ,yCAAI,CAAA;IACJ,6CAAM,CAAA;IACN,qCAAE,CAAA;IACF,0CAAI,CAAA;IACJ,wCAAG,CAAA;IACH,8CAAM,CAAA;IACN,wCAAG,CAAA;IACH,8CAAM,CAAA;IACN,4CAAK,CAAA;AACT,CAAC,EAjBI,SAAS,KAAT,SAAS,QAiBb;AAYD,wBAAwB;AACxB,SAAgB,SAAS,CAAC,GAAS;IAC/B,mBAAmB;IACnB,IAAI,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,EAAC;QACnB,OAAO,GAAG,GAAG,GAAG,CAAC,GAAG,CAAC,CAAC,CAAO,EAAC,EAAE,CAAA,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC;QAClE,iBAAiB;KAChB;SAAI;QACD,OAAO,GAAG,CAAC,IAAI,CAAC;KACnB;AACL,CAAC;AARD,8BAQC;AAGD,YAA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C,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;IACpE,uEAAuE;AAG3E,CAAC;AAAA,CAAC;AAEF,IAAI,EAAE,CAAC"}
|
|
@ -49,30 +49,50 @@ const tokenizer = p.buildLexer([
|
|||
[true,/^\d+[.]\d+/g , TokenKind.Flo],
|
||||
[true, /^\d+/g, TokenKind.Int],
|
||||
[true,/^[@]/g, TokenKind.At ],
|
||||
[true,/^[_\w][_\d\w]*/g, TokenKind.Id ],
|
||||
[true,/^[_a-zA-Z][_0-9a-zA-Z]*/g, TokenKind.Id ],
|
||||
[true,/^->/g , TokenKind.RArr ],
|
||||
[true, /^[;]/g, TokenKind.SColon ],
|
||||
[true, /^[(]/g, TokenKind.LPar ],
|
||||
[true, /^[)]/g, TokenKind.RPar ],
|
||||
[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],
|
||||
[true, /^([^\\]+?)/g, TokenKind.LitStr ],
|
||||
]);
|
||||
|
||||
|
||||
|
||||
/** ignore spaces ,new lines, and comments */
|
||||
//const _ = p.opt(p.alt(
|
||||
// p.tok(TokenKind.Space),
|
||||
// p.tok(TokenKind.Com),
|
||||
// )
|
||||
//);
|
||||
const _ = p.opt(p.alt(
|
||||
p.tok(TokenKind.Space),
|
||||
p.tok(TokenKind.Com),
|
||||
)
|
||||
);
|
||||
|
||||
function applyArg(value: [p.Token<TokenKind>,
|
||||
p.Token<TokenKind> | undefined,
|
||||
ASTNode,
|
||||
p.Token<TokenKind> | undefined,
|
||||
ASTNode,
|
||||
p.Token<TokenKind> | undefined,
|
||||
p.Token<TokenKind>]): AST[]{
|
||||
|
||||
let type = value[2];
|
||||
let variable = value[4];
|
||||
|
||||
return [type, variable];
|
||||
}
|
||||
|
||||
function applyID(value: p.Token<TokenKind.Id>): ASTNode {
|
||||
// extend value to ASTNode
|
||||
const newNode : ASTNode = {
|
||||
actualValue : value.text ,
|
||||
...value};
|
||||
return newNode;
|
||||
}
|
||||
|
||||
function applyInteger(value: p.Token<TokenKind.Int>): ASTNode {
|
||||
// extend value to ASTNode
|
||||
|
@ -99,13 +119,40 @@ function applyString(value: p.Token<TokenKind.Str>): ASTNode {
|
|||
|
||||
/** define all the parser sentence */
|
||||
const CONST = p.rule<TokenKind, ASTNode>();
|
||||
/*const VAR = p.rule<TokenKind, ASTNode>();
|
||||
const ARG = p.rule<TokenKind, ASTNode>();
|
||||
const VAR = p.rule<TokenKind, ASTNode>();
|
||||
const TYPE = p.rule<TokenKind, ASTNode>();
|
||||
const ARG = p.rule<TokenKind, AST[]>();
|
||||
|
||||
/*
|
||||
const EXPR = p.rule<TokenKind, AST>();
|
||||
const LETTING = p.rule<TokenKind, AST>();
|
||||
const LAMBDA = p.rule<TokenKind, AST>();
|
||||
const APPLYING = p.rule<TokenKind, AST>(); */
|
||||
|
||||
/** ARG ::= "(" TYPE VAR ")" */
|
||||
ARG.setPattern(
|
||||
p.apply(p.seq(
|
||||
p.tok(TokenKind.LPar),
|
||||
_,
|
||||
TYPE,
|
||||
_,
|
||||
VAR,
|
||||
_,
|
||||
p.tok(TokenKind.RPar),
|
||||
),applyArg)
|
||||
);
|
||||
|
||||
|
||||
/** VAR ::= ID */
|
||||
VAR.setPattern(
|
||||
p.apply(p.tok(TokenKind.Id), applyID),
|
||||
);
|
||||
/** TYPE ::= ID */
|
||||
TYPE.setPattern(
|
||||
p.apply(p.tok(TokenKind.Id), applyID),
|
||||
);
|
||||
|
||||
/** * CONST ::= FLO | STR | INT */
|
||||
CONST.setPattern(
|
||||
p.alt(
|
||||
p.apply(p.tok(TokenKind.Flo), applyFloat),
|
||||
|
@ -116,9 +163,11 @@ CONST.setPattern(
|
|||
);
|
||||
|
||||
|
||||
// parse the strings
|
||||
function mainParse(inputStr : string){
|
||||
return p.expectSingleResult(p.expectEOF(
|
||||
CONST.parse(tokenizer.parse(inputStr))));
|
||||
// ARG.parse(tokenizer.parse(inputStr))));
|
||||
}
|
||||
|
||||
|
||||
|
@ -126,7 +175,8 @@ return p.expectSingleResult(p.expectEOF(
|
|||
function main(){
|
||||
assert.strictEqual(<BigInt>mainParse("123").actualValue, 123n);
|
||||
assert.strictEqual(<BigInt>mainParse("3.14").actualValue, 3.14);
|
||||
assert.strictEqual(<BigInt>mainParse("\"foo\"").actualValue, "foo");
|
||||
assert.strictEqual(<BigInt>mainParse("\"foo\"").actualValue, "foo");
|
||||
//assert.strictEqual(astToSExp(mainParse("( int a )")), "(int a)");
|
||||
|
||||
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue