allow variable declaration to use callExpressions
This commit is contained in:
@ -325,6 +325,10 @@ function makeVariableDeclarators(
|
|||||||
const binExp = makeBinaryExpression(tokens, contentsStartToken.index);
|
const binExp = makeBinaryExpression(tokens, contentsStartToken.index);
|
||||||
init = binExp.expression;
|
init = binExp.expression;
|
||||||
lastIndex = binExp.lastIndex;
|
lastIndex = binExp.lastIndex;
|
||||||
|
} else if (nextAfterInit.token?.type === "brace" && nextAfterInit.token.value === "(") {
|
||||||
|
const callExInfo = makeCallExpression(tokens, contentsStartToken.index);
|
||||||
|
init = callExInfo.expression
|
||||||
|
lastIndex = callExInfo.lastIndex
|
||||||
} else {
|
} else {
|
||||||
init = makeLiteral(tokens, contentsStartToken.index);
|
init = makeLiteral(tokens, contentsStartToken.index);
|
||||||
}
|
}
|
||||||
@ -585,6 +589,7 @@ function makeBody(
|
|||||||
if (token.type === "whitespace") {
|
if (token.type === "whitespace") {
|
||||||
return makeBody(tokens, tokenIndex + 1, previousBody);
|
return makeBody(tokens, tokenIndex + 1, previousBody);
|
||||||
}
|
}
|
||||||
|
const nextToken = nextMeaningfulToken(tokens, tokenIndex);
|
||||||
if (
|
if (
|
||||||
token.type === "word" &&
|
token.type === "word" &&
|
||||||
(token.value === "const" || token.value === "fn")
|
(token.value === "const" || token.value === "fn")
|
||||||
@ -601,7 +606,7 @@ function makeBody(
|
|||||||
const nextThing = nextMeaningfulToken(tokens, lastIndex);
|
const nextThing = nextMeaningfulToken(tokens, lastIndex);
|
||||||
return makeBody(tokens, nextThing.index, [...previousBody, statement]);
|
return makeBody(tokens, nextThing.index, [...previousBody, statement]);
|
||||||
}
|
}
|
||||||
if (token.type === "word" && token.value === "log") {
|
if (token.type === "word" && nextToken.token.type === "brace" && nextToken.token.value === '(') {
|
||||||
const { expression, lastIndex } = makeExpressionStatement(
|
const { expression, lastIndex } = makeExpressionStatement(
|
||||||
tokens,
|
tokens,
|
||||||
tokenIndex
|
tokenIndex
|
||||||
|
@ -24,6 +24,35 @@ export const executor = (ast: Program, rootOverride: {[key: string]: any} = {}):
|
|||||||
const left = getVal(declaration.init.left);
|
const left = getVal(declaration.init.left);
|
||||||
const right = getVal(declaration.init.right);
|
const right = getVal(declaration.init.right);
|
||||||
programMemory.root[variableName] = left + right;
|
programMemory.root[variableName] = left + right;
|
||||||
|
} else if (declaration.init.type === "FunctionExpression") {
|
||||||
|
const fnInit = declaration.init;
|
||||||
|
|
||||||
|
programMemory.root[declaration.id.name] = (...args: any[]) => {
|
||||||
|
const fnMemory: { [key: string]: any } = {
|
||||||
|
root: {
|
||||||
|
...programMemory.root,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
if(args.length > fnInit.params.length) {
|
||||||
|
throw new Error(`Too many arguments passed to function ${declaration.id.name}`)
|
||||||
|
} else if (args.length < fnInit.params.length) {
|
||||||
|
throw new Error(`Too few arguments passed to function ${declaration.id.name}`)
|
||||||
|
}
|
||||||
|
fnInit.params.forEach((param, index) => {
|
||||||
|
fnMemory.root[param.name] = args[index];
|
||||||
|
});
|
||||||
|
return executor(fnInit.body, fnMemory.root);
|
||||||
|
}
|
||||||
|
} else if(declaration.init.type === "CallExpression") {
|
||||||
|
const fnName = declaration.init.callee.name;
|
||||||
|
const fnArgs = declaration.init.arguments.map((arg) => {
|
||||||
|
if(arg.type === "Literal") {
|
||||||
|
return arg.value;
|
||||||
|
} else if(arg.type === "Identifier") {
|
||||||
|
return programMemory.root[arg.name];
|
||||||
|
}
|
||||||
|
})
|
||||||
|
programMemory.root[variableName] = programMemory.root[fnName](...fnArgs);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else if (statement.type === "ExpressionStatement") {
|
} else if (statement.type === "ExpressionStatement") {
|
||||||
@ -39,6 +68,8 @@ export const executor = (ast: Program, rootOverride: {[key: string]: any} = {}):
|
|||||||
});
|
});
|
||||||
programMemory.root[functionName](...args);
|
programMemory.root[functionName](...args);
|
||||||
}
|
}
|
||||||
|
} else if(statement.type === "ReturnStatement") {
|
||||||
|
console.log("statement",statement)
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return programMemory;
|
return programMemory;
|
||||||
|
Reference in New Issue
Block a user