aboutsummaryrefslogtreecommitdiff
path: root/exp2ook.sno
diff options
context:
space:
mode:
authorRobin Haberkorn <rhaberkorn@fmsbw.de>2025-10-06 00:48:48 +0300
committerRobin Haberkorn <rhaberkorn@fmsbw.de>2025-10-06 00:48:48 +0300
commitad9e7cd5117c965222aae708f660e56d537914fc (patch)
tree580006656ec76513500170e5646e92e7819c6c0b /exp2ook.sno
downloadsnippets-ad9e7cd5117c965222aae708f660e56d537914fc.tar.gz
imported all of my Github gists from https://gist.github.com/rhaberkorn
Diffstat (limited to 'exp2ook.sno')
-rwxr-xr-xexp2ook.sno72
1 files changed, 72 insertions, 0 deletions
diff --git a/exp2ook.sno b/exp2ook.sno
new file mode 100755
index 0000000..7590ae4
--- /dev/null
+++ b/exp2ook.sno
@@ -0,0 +1,72 @@
+#!/usr/local/bin/snobol4 -b
+
+* Arithmetic expression to Brainfuck/Ook! compiler
+* ./exp2ook.sno [-bf] "expression"
+* Compiles "expression" and prints Ook! source to stdout
+* "-bf" prints the Brainfuck source only
+* The compiled programs calculate the result and display it (ASCII letters)
+
+EXP2OOK CODE('EXP2OOK')
+ DEFINE('PUSH()')
+ DEFINE('POP()')
+ DEFINE('BINOP()NEW')
+ DEFINE('COMPILE(NODE)O')
+
+ DATA('OP(R,TYPE,L)')
+
+ &ANCHOR = 1
+ &FULLSCAN = 1
+
+ SPACE = SPAN(" ") | ""
+ PRE = SPACE ("(" *EXP SPACE ")" | SPAN("0123456789") $ *PUSH())
+ POST = SPACE (ANY("+-") $ *PUSH() *EXP *BINOP() |
++ ANY("*/") $ *PUSH() PRE *BINOP() *POST) | ""
+ EXP = PRE POST
+
+ I = HOST(3)
+L.3 HOST(2, I) "-" :F(L.4)
+ I = I + 1 :(L.3)
+L.4 HOST(2, I) :S(L.5)
+ TERMINAL = "No expression specified!" :(END)
+
+L.5 STACK = ARRAY(10)
+ SP = 0
+
+ TRIM(HOST(2, I)) EXP RPOS(0) :S(L.6)
+ TERMINAL = "Invalid expression!" :(END)
+
+L.6 SRC = COMPILE(STACK<1>)
++ "[>++++++++++<[->->+>>+<<<[>>+>[-]<<<-]>>[<<+>>-]"
++ ">[-<<[<+>-]>>>+<]<<<<]>>>>>[<<<<<+>>>>>-]>[>]" DUPL("+",ORD("0"))
++ "[<]<<<[>>>>[>]<+[<]<<<-]<[-]<]>>>>>>[>]<[.<]"
+
+ LEQ(HOST(2, HOST(3)), "-bf") :F(L.9)
+ OUTPUT = SRC :(END)
+
+L.9 OUTPUT(.TERM, 6, "T")
+ SRC ARBNO("+" *?(TERM = "Ook. Ook. ") |
++ "-" *?(TERM = "Ook! Ook! ") |
++ ">" *?(TERM = "Ook. Ook? ") |
++ "<" *?(TERM = "Ook? Ook. ") |
++ "[" *?(TERM = "Ook! Ook? ") |
++ "]" *?(TERM = "Ook? Ook! ") |
++ "." *?(TERM = "Ook! Ook. ") | LEN(1)) RPOS(0) :(END)
+
+* PROCEDURES
+
+PUSH PUSH = .STACK<SP = SP + 1> :(NRETURN)
+POP POP = STACK<SP>
+ SP = SP - 1 :(RETURN)
+
+BINOP NEW = OP(POP(), POP(), POP())
+ PUSH() = NEW :(RETURN)
+
+COMPILE LEQ(DATATYPE(NODE), .OP) :S(L.8)
+ COMPILE = DUPL("+", NODE) :(RETURN)
+L.8 COMPILE = COMPILE(L(NODE)) ">" COMPILE(R(NODE))
+ TYPE(NODE) ANY("+-") $ O *?(COMPILE = COMPILE "[<" O ">-]<") |
++ "*" *?(COMPILE = ">>" COMPILE "[<[<+<+>>-]<[>+<-]>>-]<[-]<<") |
++ "/" *?(COMPILE = ">" COMPILE "<[->->+>>+<<<[>>+>[-]<<<-]>>"
++ "[<<+>>-]>[-<<[<+>-]<<<+>>>>>]<<<<]>[-]>[-]<<<") :(RETURN)
+
+END EXP2OOK