local ds = require("DataStructures") local Dictionary, Stack, WordInfo, State = ds.Dictionary, ds.Stack, ds.WordInfo, ds.State local helpers = require("CoreHelpers") local areNumbers, getActiveDataStack, isNumber, isWhitespace= helpers.areNumbers, helpers.getActiveDataStack, helpers.isNumber, helpers.isWhitespace local skipWhitespace, parseToken = helpers.skipWhitespace, helpers.parseToken local popTwoOperands = helpers.popTwoOperands -- Mathematical operations local function add(state: State) local a, b = popTwoOperands(state) if areNumbers(a,b) then local c = (a as number) + (b as number) getActiveDataStack(state):push(c) else error("invalid operands for add operation!") end end local function sub(state: State) local a, b = popTwoOperands(state) if areNumbers(a, b) then local c = (a as number) - (b as number) getActiveDataStack(state):push(c) else error("invalid operands for sub operation!") end end local function mul(state: State) local a, b = popTwoOperands(state) if areNumbers(a, b) then local c = (a as number) * (b as number) getActiveDataStack(state):push(c) else error("invalid operands for mul operation!") end end local function div(state: State) local a, b = popTwoOperands(state) if areNumbers(a, b) then local c = (a as number) / (b as number) getActiveDataStack(state):push(c) else error("invalid operands for div operation!") end end -- I/O operations local function dot(state: State) local out = state.activeDataStack:pop() print(out) end local CoreWords = Dictionary:new() local addInfo = WordInfo:new(add, false) local subInfo = WordInfo:new(sub, false) local mulInfo = WordInfo:new(mul, false) local divInfo = WordInfo:new(div, false) local dotInfo = WordInfo:new(dot, false) CoreWords:define("+", addInfo) CoreWords:define("-", subInfo) CoreWords:define("*", mulInfo) CoreWords:define("/", divInfo) CoreWords:define(".", dotInfo) return CoreWords