remove semi-colons

This commit is contained in:
Kurt Hutten IrevDev
2022-11-26 08:34:23 +11:00
parent ab9fb05e30
commit 48e59ac710
19 changed files with 1159 additions and 1122 deletions

View File

@ -1,16 +1,16 @@
import { Program, BinaryPart, BinaryExpression } from "./abstractSyntaxTree";
import {Path, sketchFns } from "./sketch";
import { Program, BinaryPart, BinaryExpression } from './abstractSyntaxTree'
import { Path, sketchFns } from './sketch'
export interface ProgramMemory {
root: { [key: string]: any };
return?: any;
_sketch: Path[];
root: { [key: string]: any }
return?: any
_sketch: Path[]
}
export const executor = (
node: Program,
programMemory: ProgramMemory = { root: {}, _sketch: [] },
options: { bodyType: "root" | "sketch" | "block" } = { bodyType: "root" }
options: { bodyType: 'root' | 'sketch' | 'block' } = { bodyType: 'root' }
): any => {
const _programMemory: ProgramMemory = {
root: {
@ -18,33 +18,33 @@ export const executor = (
},
_sketch: [],
return: programMemory.return,
};
const { body } = node;
}
const { body } = node
body.forEach((statement) => {
if (statement.type === "VariableDeclaration") {
if (statement.type === 'VariableDeclaration') {
statement.declarations.forEach((declaration) => {
const variableName = declaration.id.name;
if (declaration.init.type === "Literal") {
_programMemory.root[variableName] = declaration.init.value;
} else if (declaration.init.type === "BinaryExpression") {
const variableName = declaration.id.name
if (declaration.init.type === 'Literal') {
_programMemory.root[variableName] = declaration.init.value
} else if (declaration.init.type === 'BinaryExpression') {
_programMemory.root[variableName] = getBinaryExpressionResult(
declaration.init,
_programMemory
);
} else if (declaration.init.type === "SketchExpression") {
const sketchInit = declaration.init;
)
} else if (declaration.init.type === 'SketchExpression') {
const sketchInit = declaration.init
const fnMemory: ProgramMemory = {
root: {
..._programMemory.root,
},
_sketch: [],
};
}
const { _sketch } = executor(sketchInit.body, fnMemory, {
bodyType: "sketch",
});
_programMemory.root[variableName] = _sketch;
} else if (declaration.init.type === "FunctionExpression") {
const fnInit = declaration.init;
bodyType: 'sketch',
})
_programMemory.root[variableName] = _sketch
} else if (declaration.init.type === 'FunctionExpression') {
const fnInit = declaration.init
_programMemory.root[declaration.id.name] = (...args: any[]) => {
const fnMemory: ProgramMemory = {
@ -52,106 +52,108 @@ export const executor = (
..._programMemory.root,
},
_sketch: [],
};
}
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, {bodyType: 'block'}).return;
};
} else if (declaration.init.type === "CallExpression") {
const fnName = declaration.init.callee.name;
fnMemory.root[param.name] = args[index]
})
return executor(fnInit.body, fnMemory, { bodyType: 'block' }).return
}
} 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];
if (arg.type === 'Literal') {
return arg.value
} else if (arg.type === 'Identifier') {
return _programMemory.root[arg.name]
}
});
if ("lineTo" === fnName || "close" === fnName) {
if (options.bodyType !== "sketch") {
})
if ('lineTo' === fnName || 'close' === fnName) {
if (options.bodyType !== 'sketch') {
throw new Error(
`Cannot call ${fnName} outside of a sketch declaration`
);
)
}
const result = sketchFns[fnName](
_programMemory,
variableName,
[declaration.start, declaration.end],
...fnArgs
);
_programMemory._sketch = result.programMemory._sketch;
_programMemory.root[variableName] = result.currentPath;
)
_programMemory._sketch = result.programMemory._sketch
_programMemory.root[variableName] = result.currentPath
} else {
_programMemory.root[variableName] = _programMemory.root[fnName](
...fnArgs
);
)
}
}
});
} else if (statement.type === "ExpressionStatement") {
const expression = statement.expression;
if (expression.type === "CallExpression") {
const functionName = expression.callee.name;
})
} else if (statement.type === 'ExpressionStatement') {
const expression = statement.expression
if (expression.type === 'CallExpression') {
const functionName = expression.callee.name
const args = expression.arguments.map((arg) => {
if (arg.type === "Literal") {
return arg.value;
} else if (arg.type === "Identifier") {
return _programMemory.root[arg.name];
if (arg.type === 'Literal') {
return arg.value
} else if (arg.type === 'Identifier') {
return _programMemory.root[arg.name]
}
});
if ("lineTo" === functionName || "close" === functionName) {
if (options.bodyType !== "sketch") {
})
if ('lineTo' === functionName || 'close' === functionName) {
if (options.bodyType !== 'sketch') {
throw new Error(
`Cannot call ${functionName} outside of a sketch declaration`
);
)
}
const result = sketchFns[functionName](_programMemory, "", [statement.start, statement.end], ...args);
_programMemory._sketch = [...result.programMemory._sketch];
} else if("show" === functionName) {
if (options.bodyType !== "root") {
throw new Error(
`Cannot call ${functionName} outside of a root`
);
const result = sketchFns[functionName](
_programMemory,
'',
[statement.start, statement.end],
...args
)
_programMemory._sketch = [...result.programMemory._sketch]
} else if ('show' === functionName) {
if (options.bodyType !== 'root') {
throw new Error(`Cannot call ${functionName} outside of a root`)
}
_programMemory.return = expression.arguments;
_programMemory.return = expression.arguments
} else {
_programMemory.root[functionName](...args);
_programMemory.root[functionName](...args)
}
}
} else if (statement.type === "ReturnStatement") {
if (statement.argument.type === "BinaryExpression") {
} else if (statement.type === 'ReturnStatement') {
if (statement.argument.type === 'BinaryExpression') {
_programMemory.return = getBinaryExpressionResult(
statement.argument,
_programMemory
);
)
}
}
});
return _programMemory;
};
})
return _programMemory
}
function getBinaryExpressionResult(
expression: BinaryExpression,
programMemory: ProgramMemory
) {
const getVal = (part: BinaryPart) => {
if (part.type === "Literal") {
return part.value;
} else if (part.type === "Identifier") {
return programMemory.root[part.name];
if (part.type === 'Literal') {
return part.value
} else if (part.type === 'Identifier') {
return programMemory.root[part.name]
}
};
const left = getVal(expression.left);
const right = getVal(expression.right);
return left + right;
}
const left = getVal(expression.left)
const right = getVal(expression.right)
return left + right
}