123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 |
- import { Store } from './store/store';
- import { StoreObject } from './store/storeObject';
- import { StoreObjectArray } from './store/storeObjectArray';
- import { Modes } from './mode';
- import { Context } from './context';
- import { Types } from './../ast/types';
- import * as Commands from './../ast/commands/';
- import * as Expressions from './../ast/expressions/';
- export class IVProgProcessor {
- constructor(ast) {
- this.ast = ast;
- this.globalStore = new Store();
- this.stores = [this.globalStore];
- this.context = [Context.BASE];
- this.input = null;
- this.output = null;
- }
- registerInput (input) {
- this.input = input;
- }
- registerOutput (output) {
- this.output = output;
- }
- checkContext(context) {
- return this.context[this.context.length] === context;
- }
- ignoreSwitchCases (store) {
- if (store.mode === Modes.RETURN) {
- return true;
- } else if (store.mode === Modes.BREAK) {
- return true;
- } else {
- return false;
- }
- }
- interpretAST () {
- this.initGlobal();
- const mainFunc = this.findMainFunction();
- if(mainFunc === null) {
-
- throw new Error("Missing main funciton.");
- }
- return this.runFunction(mainFunc, [], this.globalStore);
- }
- initGlobal () {
- if(!this.checkContext(Context.BASE)) {
- throw new Error("!!!CRITICAL: Invalid call to initGlobal outside BASE context!!!");
- }
- this.ast.global.forEach(decl => {
- this.executeCommand(this.globalStore, decl).then(v => this.globalStore = v);
- });
- }
- findMainFunction () {
- return this.ast.functions.find(v => v.isMain);
- }
- findFunction (name) {
- const val = this.ast.functions.find( v => v.name === name);
- if (!!!val) {
-
- throw new Error(`Function ${name} is not defined.`);
- }
- return val;
- }
- runFunction (func, actualParameters, store) {
- let funcStore = new Store();
- funcStore.extendStore(this.globalStore);
- const returnStoreObject = new StoreObject(func.returnType, null);
- const funcName = func.isMain ? 'main' : func.name;
- const funcNameStoreObject = new StoreObject(Types.STRING, funcName, true);
- funcStore.insertStore('$', returnStoreObject);
- funcStore.insertStore('$name', funcNameStoreObject);
- funcStore = this.associateParameters(func.formalParameters, actualParameters, store, funcStore);
- this.context.push(Context.FUNCTION);
- this.stores.push(funcStore);
- const result = this.executeCommands(funcStore, func.commands);
- this.stores.pop();
- this.context.pop();
- return result;
- }
- associateParameters (formalList, actualList, callerStore, calleeStore) {
- if (formalList.length != actualList.length) {
-
- throw new Error("Numbers of parameters doesn't match");
- }
- formalList.forEach((v, i) => {
- const val = this.evaluateExpression(callerStore, actualList[i]);
- switch (v.dimensions) {
- case 1: {
- if (val.lines > 0 && val.columns === null) {
- calleeStore.insertStore(v.id, val);
- } else {
-
- throw new Error(`Parameter ${v.id} is not compatible with the value given.`);
- }
- break;
- }
- case 2: {
- if (val.lines > 0 && val.columns > 0) {
- calleeStore.insertStore(v.id, val);
- } else {
-
- throw new Error(`Parameter ${v.id} is not compatible with the value given.`);
- }
- break;
- }
- case 0: {
- if (val.type !== v.type) {
-
- throw new Error(`Parameter ${v.id} is not compatible with ${val.type}.`);
- } else {
- calleeStore.insertStore(v.id, val);
- }
- }
- }
- });
- return calleeStore;
- }
- executeCommands (store, cmds) {
- return cmds.reduce((promise, cmd) => promise.then( sto => {
- while (sto.mode === Modes.PAUSE) {
- continue;
- }
- if(sto.mode === Modes.RETURN) {
- return Promise.resolve(sto);
- } else if (this.checkContext(Context.BREAKABLE &&
- sto.mode === Modes.BREAK)) {
- return Promise.resolve(sto);
- }
- return this.executeCommand(sto, cmd);
- }), Promise.resolve(store));
- }
- executeCommand (store, cmd) {
- while (store.mode === Modes.PAUSE) {
- continue;
- }
- if(store.mode === Modes.RETURN) {
- return Promise.resolve(store);
- } else if(this.checkContext(Context.BREAKABLE) && store.mode === Modes.BREAK) {
- return Promise.resolve(store);
- }
- if (cmd instanceof Commands.Declaration) {
- return this.executeDeclaration(store, cmd);
- } else if (cmd instanceof Commands.Assign) {
- return this.executeAssign(store, cmd);
- } else if (cmd instanceof Commands.Break) {
- return this.executeBreak(store, cmd);
- } else if (cmd instanceof Commands.Return) {
- return this.executeReturn(store, cmd);
- } else if (cmd instanceof Commands.IfThenElse) {
- return this.executeIfThenElse(store, cmd);
- } else if (cmd instanceof Commands.While) {
- return this.executeWhile(store, cmd);
- } else if (cmd instanceof Commands.DoWhile) {
- return this.executeDoWhile(store, cmd);
- } else if (cmd instanceof Commands.For) {
- return this.executeFor(store, cmd);
- } else if (cmd instanceof Commands.Switch) {
- return this.executeSwitch(store, cmd);
- } else if (cmd instanceof Commands.FunctionCall) {
- return this.executeFunctionCall(store, cmd);
- } else {
- throw new Error("!!!CRITICAL A unknown command was found!!!\n" + cmd);
- }
- }
- executeFunctionCall (store, cmd) {
- const func = this.findFunction(cmd.id);
- this.runFunction(func, cmd.actualParameters, store);
- return Promise.resolve(store);
- }
- executeSwitch (store, cmd) {
- const auxCaseFun = (promise, switchExp, aCase) => {
- return promise.then( result => {
- const sto = result.sto;
- if (this.ignoreSwitchCases(sto)) {
- return Promise.resolve(result);
- } else if (result.wasTrue || aCase.isDefault) {
- const newSto = this.executeCommand(result.sto,aCase.commands);
- return Promise.resolve({wasTrue: true, sto: newSto});
- } else {
- const value = this.evaluateExpression(sto,
- new Expressions.InfixApp('==', switchExp, aCase.expression));
- if (value.value) {
- const newSto = this.executeCommand(result.sto,aCase.commands);
- return Promise.resolve({wasTrue: true, sto: newSto});
- } else {
- return Promise.resolve({wasTrue: false, sto: newSto});
- }
- }
- });
- }
- try {
- this.context.push(Context.BREAKABLE);
- let breakLoop = false;
- const case0 = cmd.cases[0];
- let result = auxCaseFun(Promise.resolve({wasTrue: false, sto: store}),
- cmd.expression,
- case0);
- for (let index = 1; index < cmd.cases.length && !breakLoop; index++) {
- const aCase = cmd.cases[index];
- result = auxCaseFun(result, cmd.expression, aCase);
- result.then( r => breakLoop = this.ignoreSwitchCases(r.sto));
- }
- this.context.pop();
- return result.then(r => r.sto);
- } catch (error) {
- return Promise.reject(error);
- }
- }
- executeFor (store, cmd) {
- try {
-
- const initCmd = cmd.assignment;
- const condition = cmd.condition;
- const increment = cmd.increment;
- const whileBlock = new Commands.CommandBlock([],
- cmd.commands.concat(increment));
- const forAsWhile = new Commands.While(condition, whileBlock);
-
- const newCmdList = [initCmd,forAsWhile];
- return Promise.resolve(this.executeCommands(store, newCmdList));
- } catch (error) {
- return Promise.reject(error);
- }
- }
- executeDoWhile (store, cmd) {
- try {
- this.context.push(Context.BREAKABLE);
- const newStore = this.executeCommands(store, cmd.commands);
- const value = this.evaluateExpression(newStore, cmd.expression);
- if (value.type !== Types.BOOLEAN) {
-
-
- return Promise.reject(new Error(`DoWhile expression must be of type boolean`));
- }
- if (value.value) {
- return Promise.resolve(this.executeCommand(newStore, cmd));
- } else {
- return Promise.resolve(newStore);
- }
- } catch (error) {
- return Promise.reject(error)
- }
- }
- executeWhile (store, cmd) {
- try {
- this.context.push(Context.BREAKABLE);
- const value = this.evaluateExpression(store, cmd.expression);
- if(value.type === Types.BOOLEAN) {
- if(value.value) {
- const newStore = this.executeCommands(store, cmd.commands);
- this.context.pop();
- return Promise.resolve(this.executeCommand(newStore, cmd));
- } else {
- this.context.pop();
- return Promise.resolve(store);
- }
- } else {
-
-
- return Promise.reject(new Error(`Loop condition must be of type boolean`));
- }
- } catch (error) {
- return Promise.reject(error);
- }
- }
- executeIfThenElse (store, cmd) {
- try {
- const value = this.evaluateExpression(cmd.condition);
- if(value.type === Types.BOOLEAN) {
- if(value.value) {
- return Promise.resolve(this.executeCommand(store, cmd.ifTrue));
- } else {
- return Promise.resolve(this.executeCommand(store, cmd.ifFalse));
- }
- } else {
-
-
- return Promise.reject(new Error(`If expression must be of type boolean`));
- }
- } catch (error) {
- return Promise.reject(error);
- }
- }
- executeReturn (store, cmd) {
- try {
- const funcType = store.applyStore('$');
- const value = this.evaluateExpression(store, cmd.expression);
- const funcName = store.applyStore('$name');
- if (funcType.type !== value.type) {
-
-
- return Promise.reject(new Error(`Function ${funcName.value} must return ${funcType.type} instead of ${value.type}.`));
- } else {
- store.updateStore('$', value);
- store.mode = Modes.RETURN;
- return Promise.resolve(store);
- }
- } catch (error) {
- return Promise.reject(error);
- }
- }
- executeBreak (store, cmd) {
- if(this.checkContext(Context.BREAKABLE)) {
- store.mode = Modes.BREAK;
- return Promise.resolve(store);
- } else {
- return Promise.reject(new Error("!!!CRITIAL: Break command outside Loop/Switch scope!!!"));
- }
- }
- executeAssign (store, cmd) {
- try {
- const value = this.evaluateExpression(store, cmd.expression);
- store.updateStore(cmd.id, value);
- return Promise.resolve(store);
- } catch (error) {
- return Promise.reject(error);
- }
- }
- executeDeclaration (store, cmd) {
- try {
- const value = this.evaluateExpression(store, cmd.initial);
- if(cmd instanceof Commands.ArrayDeclaration) {
- const temp = new StoreObjectArray(decl.subtype, decl.lines, decl.columns, null, decl.isConst);
- store.insertStore(decl.id, temp);
- store.updateStore(decl.id, value);
- } else {
- const temp = new StoreObject(decl.type, null, decl.isConst);
- store.insertStore(decl.id, temp);
- store.updateStore(decl.id, value);
- }
- return Promise.resolve(store);
- } catch (e) {
- return Promise.reject(e);
- }
- }
- }
|