forth-stuff/CoreWords.tl

214 lines
5.6 KiB
Plaintext
Raw Normal View History

2021-05-12 08:32:25 +00:00
local ds = require("DataStructures")
2021-05-14 08:29:54 +00:00
local Dictionary, Stack, WordInfo, Environment = ds.Dictionary, ds.Stack, ds.WordInfo, ds.Environment
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
2021-05-13 10:54:06 +00:00
local defineWord = helpers.defineWord
local ret = helpers.ret
local makeCall = helpers.makeCall
2021-05-11 08:19:54 +00:00
-- Mathematical operations
2021-05-14 08:29:54 +00:00
local function add(state: Environment)
2021-05-11 08:19:54 +00:00
local a, b = popTwoOperands(state)
if areNumbers(a,b) then
2021-05-11 08:19:54 +00:00
local c = (a as number) + (b as number)
getActiveDataStack(state):push(c)
2021-05-11 06:58:09 +00:00
else
error("invalid operands for add operation!")
end
end
2021-05-14 08:29:54 +00:00
local function sub(state: Environment)
2021-05-11 08:19:54 +00:00
local a, b = popTwoOperands(state)
if areNumbers(a, b) then
2021-05-11 08:19:54 +00:00
local c = (a as number) - (b as number)
getActiveDataStack(state):push(c)
else
error("invalid operands for sub operation!")
end
2021-05-11 06:58:09 +00:00
end
2021-05-14 08:29:54 +00:00
local function mul(state: Environment)
2021-05-11 08:19:54 +00:00
local a, b = popTwoOperands(state)
if areNumbers(a, b) then
2021-05-11 08:19:54 +00:00
local c = (a as number) * (b as number)
getActiveDataStack(state):push(c)
else
error("invalid operands for mul operation!")
end
end
2021-05-14 08:29:54 +00:00
local function div(state: Environment)
2021-05-11 08:19:54 +00:00
local a, b = popTwoOperands(state)
if areNumbers(a, b) then
2021-05-11 08:19:54 +00:00
local c = (a as number) / (b as number)
getActiveDataStack(state):push(c)
else
error("invalid operands for div operation!")
end
end
2021-05-13 10:54:06 +00:00
2021-05-14 08:29:54 +00:00
local function dup(state: Environment)
2021-05-13 10:54:06 +00:00
local stack = getActiveDataStack(state)
local top = stack:pop()
stack:push(top)
stack:push(top)
end
2021-05-14 08:29:54 +00:00
local function swap(state: Environment)
2021-05-13 10:54:06 +00:00
local stack = getActiveDataStack(state)
local a, b = popTwoOperands(state)
stack:push(b)
stack:push(a)
end
2021-05-14 08:29:54 +00:00
local function rot(state: Environment)
2021-05-13 10:54:06 +00:00
local stack = getActiveDataStack(state)
local c, b, a= stack:pop(), stack:pop(), stack:pop()
stack:push(b)
stack:push(c)
stack:push(a)
end
2021-05-14 08:29:54 +00:00
local function drop(state: Environment)
2021-05-13 10:54:06 +00:00
local stack = getActiveDataStack(state)
stack:pop()
end
2021-05-14 08:29:54 +00:00
local function over(state: Environment)
2021-05-13 10:54:06 +00:00
local stack = getActiveDataStack(state)
local b, a = stack:pop(), stack:pop()
stack:push(a)
stack:push(b)
stack:push(a)
end
2021-05-11 08:19:54 +00:00
-- I/O operations
2021-05-14 08:29:54 +00:00
local function dot(state: Environment)
2021-05-11 08:19:54 +00:00
local out = state.activeDataStack:pop()
print(out)
2021-05-13 10:54:06 +00:00
end
2021-05-14 08:29:54 +00:00
local function twoDup(state: Environment)
2021-05-13 10:54:06 +00:00
over(state)
over(state)
end
2021-05-14 08:29:54 +00:00
local function twoSwap(state: Environment)
2021-05-13 10:54:06 +00:00
local stack = getActiveDataStack(state)
local d, c, b, a = stack:pop(), stack:pop(), stack:pop(), stack:pop()
stack:push(c)
stack:push(d)
stack:push(a)
stack:push(b)
end
2021-05-11 08:19:54 +00:00
2021-05-14 08:29:54 +00:00
local function twoOver(state: Environment)
2021-05-13 10:54:06 +00:00
local stack = getActiveDataStack(state)
local d, c, b, a = stack:pop(), stack:pop(), stack:pop(), stack:pop()
stack:push(a)
stack:push(b)
stack:push(c)
stack:push(d)
stack:push(a)
stack:push(b)
end
2021-05-14 08:29:54 +00:00
local function nip(state: Environment)
2021-05-13 10:54:06 +00:00
local stack = getActiveDataStack(state)
local b, _ = stack:pop(), stack:pop()
stack:push(b)
2021-05-11 08:19:54 +00:00
end
2021-05-14 08:29:54 +00:00
local function tuck(state: Environment)
2021-05-13 10:54:06 +00:00
swap(state)
over(state)
end
2021-05-14 08:29:54 +00:00
local function roll(state: Environment)
2021-05-13 10:54:06 +00:00
local stack = getActiveDataStack(state)
local u = stack:pop()
local bufferStack = Stack:new()
if u is number then
local v = u as number
while(v > 0) do
local item = stack:pop()
bufferStack:push(item)
v = v - 1
end
local newTop = stack:pop()
local x = (u as number)
while(x > 0) do
local item = bufferStack:pop()
stack:push(item)
x = x - 1
end
stack:push(newTop)
else
error("u is not a number")
end
end
2021-05-14 08:29:54 +00:00
local function getExecutionToken(state: Environment)
2021-05-13 11:09:05 +00:00
local stack = getActiveDataStack(state)
skipWhitespace(state)
local name: string = parseToken(state)
for _, dictionary in ipairs(state.dictionaries) do
local wordinfo = dictionary:lookup(name)
if wordinfo then
stack:push((wordinfo as WordInfo).func)
break
end
end
end
2021-05-13 10:54:06 +00:00
2021-05-14 08:29:54 +00:00
local function execute(state: Environment)
2021-05-13 11:09:05 +00:00
local stack = getActiveDataStack(state)
2021-05-14 08:29:54 +00:00
local func: function(Environment) = stack:pop() as function(Environment)
2021-05-13 11:09:05 +00:00
func(state)
end
2021-05-11 08:19:54 +00:00
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)
2021-05-13 10:54:06 +00:00
local dupInfo = WordInfo:new(dup, false)
local swapInfo = WordInfo:new(swap, false)
local rotInfo = WordInfo:new(rot, false)
local dropInfo = WordInfo:new(drop, false)
local overInfo = WordInfo:new(over, false)
local twoDupInfo = WordInfo:new(twoDup, false)
local twoSwapInfo = WordInfo:new(twoSwap, false)
local twoOverInfo = WordInfo:new(twoOver, false)
local nipInfo = WordInfo:new(nip, false)
defineWord(CoreWords, "TUCK", tuck, false)
defineWord(CoreWords, "ROLL", roll, false)
2021-05-13 11:09:05 +00:00
defineWord(CoreWords, "'", getExecutionToken, false)
defineWord(CoreWords, "EXECUTE", execute, false)
2021-05-13 10:54:06 +00:00
CoreWords:define("+", addInfo)
CoreWords:define("-", subInfo)
CoreWords:define("*", mulInfo)
CoreWords:define("/", divInfo)
CoreWords:define(".", dotInfo)
2021-05-13 10:54:06 +00:00
CoreWords:define("DUP", dupInfo)
CoreWords:define("SWAP", swapInfo)
CoreWords:define("ROT", rotInfo)
CoreWords:define("DROP", dropInfo)
CoreWords:define("OVER", overInfo)
CoreWords:define("2DUP", twoDupInfo)
CoreWords:define("2SWAP", twoSwapInfo)
CoreWords:define("2OVER", twoOverInfo)
CoreWords:define("NIP", nipInfo)
local sqr = {dup, mul, ret}
local sqrf = makeCall(sqr)
defineWord(CoreWords, "SQR", sqrf, false)
2021-05-11 08:19:54 +00:00
return CoreWords
2021-05-11 06:55:13 +00:00