diff --git a/interpret.js b/interpret.js index eceaedd..126056b 100644 --- a/interpret.js +++ b/interpret.js @@ -1,14 +1,17 @@ let program = [ - - { print: 'hello' }, - { print: 'world' } - + { + print: { '+': [3,5] } + }, ] let builtins = { print: (text) => { console.log(text) } } +let binaryOperators = { + '+': (a,b) => { return a+b }, +} + interpret = (program) => { for (let stmt of program) { exec(stmt) @@ -16,12 +19,21 @@ interpret = (program) => { }; exec = (stmt) => { + + if(typeof stmt === 'number' || typeof stmt === 'string'){ + return stmt; + } + let key = Object.keys(stmt)[0] if (typeof builtins[key] === 'function') { - builtins[key](stmt[key]) + builtins[key](exec(stmt[key])) + } else if (typeof binaryOperators[key] === 'function') { + let firstArgument = stmt[key][0]; + let secondArgument = stmt[key][1]; + return binaryOperators[key](firstArgument, secondArgument) }else{ console.error('unknown instruction: '+key) } } -interpret(program) +interpret(program) \ No newline at end of file