Almost finito, just missing adding the *

This commit is contained in:
Rolf Martin Glomsrud 2024-05-15 21:11:52 +02:00
parent 4c8d17311f
commit 2b053f5b46
3 changed files with 16 additions and 22 deletions

View file

@ -2,7 +2,7 @@ proposal DoExpression{
pair arrowFunction{ pair arrowFunction{
applicable to { applicable to {
"let <<ident:Identifier>> = () => { "let <<ident:Identifier>> = () => {
<<statements: NMinus1Statements >> <<statements: Statement && !ReturnStatement *>>
return <<returnVal : Expression | Identifier>>; return <<returnVal : Expression | Identifier>>;
} }
" "
@ -18,7 +18,7 @@ proposal DoExpression{
pair immediatelyInvokedUnnamedFunction { pair immediatelyInvokedUnnamedFunction {
applicable to { applicable to {
"let <<ident:Identifier>> = function(){ "let <<ident:Identifier>> = function(){
<<statements: NMinus1Statements >> <<statements: Statement && !ReturnStatement >>
return <<returnVal : Expression | Identifier>>; return <<returnVal : Expression | Identifier>>;
}();" }();"
} }

View file

@ -118,7 +118,7 @@ export interface GroupExpr extends WildcardNode {
expr: TypeExpr; expr: TypeExpr;
} }
class WildcardParser { export class WildcardParser {
private position = -1; private position = -1;
constructor(private tokens: WildcardToken[]) {} constructor(private tokens: WildcardToken[]) {}

View file

@ -1,14 +1,15 @@
import { InternalDSLVariable, parse_with_plugins } from "./parse"; import { Wildcard, WildcardParser, parse_with_plugins } from "./parse";
import * as t from "@babel/types"; import * as t from "@babel/types";
import { WildcardTokenizer } from "./wildcardTokenizer";
export function preludeBuilder(prelude: string) { export function preludeBuilder(prelude: string) {
let parsedPrelude = parse_with_plugins(prelude).program.body; let parsedPrelude = parse_with_plugins(prelude).program.body;
return extractValues(parsedPrelude); return extractValues(parsedPrelude);
} }
function extractValues(types: t.Statement[]): InternalDSLVariable { function extractValues(types: t.Statement[]): Wildcard[] {
let prelude: InternalDSLVariable = {}; let prelude: Wildcard[] = [];
for (let stmt of types) { for (let stmt of types) {
if (stmt.type == "VariableDeclaration") { if (stmt.type == "VariableDeclaration") {
stmt = <t.VariableDeclaration>stmt; stmt = <t.VariableDeclaration>stmt;
@ -16,22 +17,15 @@ function extractValues(types: t.Statement[]): InternalDSLVariable {
let innerDSLVariableName = (declaration.id as t.Identifier).name; let innerDSLVariableName = (declaration.id as t.Identifier).name;
let init = declaration.init; let init = declaration.init;
if (init) { if (init) {
if (init.type == "ArrayExpression") { if (init.type == "StringLiteral") {
init = <t.ArrayExpression>init; init = <t.StringLiteral>init;
let temp = []; prelude.push(
for (let elem of init.elements) { new WildcardParser(
if (elem && elem.type == "Identifier") { new WildcardTokenizer(
temp.push(elem.name); innerDSLVariableName + ":" + init
} else { ).tokenize()
throw new Error( ).parse()
"Usage of non variable declaration in Prelude Array" );
);
}
}
prelude[innerDSLVariableName] = temp;
} else if (init.type === "Identifier") {
init = <t.Identifier>init;
prelude[innerDSLVariableName] = [init.name];
} else { } else {
throw new Error( throw new Error(
"Invalid usage of right side declaration in prelude" "Invalid usage of right side declaration in prelude"