forked from ground/ground
Example calculator
This commit is contained in:
107
docs/demos/calc.grnd
Executable file
107
docs/demos/calc.grnd
Executable file
@@ -0,0 +1,107 @@
|
|||||||
|
#!/usr/bin/env ground
|
||||||
|
|
||||||
|
@begin
|
||||||
|
stdout "Calculation: "
|
||||||
|
stdin &calc
|
||||||
|
getstrsize $calc &calcsize
|
||||||
|
set &counter 0
|
||||||
|
set &left ""
|
||||||
|
set &right ""
|
||||||
|
set &operator ' '
|
||||||
|
set &isRight false
|
||||||
|
|
||||||
|
## Preprocessing
|
||||||
|
|
||||||
|
# Loop to parse input
|
||||||
|
@loopstart
|
||||||
|
getstrcharat $calc $counter &char
|
||||||
|
|
||||||
|
# Remove any spaces (they're inconvenient)
|
||||||
|
equal $char ' ' &cond
|
||||||
|
if $cond %doneif
|
||||||
|
|
||||||
|
# Check for operators
|
||||||
|
# '+' operator
|
||||||
|
inequal '+' $char &cond
|
||||||
|
if $cond %minusOpCheck
|
||||||
|
set &operator $char
|
||||||
|
set &isRight true
|
||||||
|
jump %doneif
|
||||||
|
|
||||||
|
@minusOpCheck
|
||||||
|
# '-' operator
|
||||||
|
inequal '-' $char &cond
|
||||||
|
if $cond %multiplyOpCheck
|
||||||
|
set &operator $char
|
||||||
|
set &isRight true
|
||||||
|
jump %doneif
|
||||||
|
|
||||||
|
@multiplyOpCheck
|
||||||
|
# '*' operator
|
||||||
|
inequal '*' $char &cond
|
||||||
|
if $cond %divideOpCheck
|
||||||
|
set &operator $char
|
||||||
|
set &isRight true
|
||||||
|
jump %doneif
|
||||||
|
|
||||||
|
@divideOpCheck
|
||||||
|
# '/' operator
|
||||||
|
inequal '/' $char &cond
|
||||||
|
if $cond %endOpChecks
|
||||||
|
set &operator $char
|
||||||
|
set &isRight true
|
||||||
|
jump %doneif
|
||||||
|
|
||||||
|
@endOpChecks
|
||||||
|
if $isRight %isRight
|
||||||
|
add $left $char &left
|
||||||
|
jump %doneif
|
||||||
|
@isRight
|
||||||
|
add $right $char &right
|
||||||
|
@doneif
|
||||||
|
add 1 $counter &counter
|
||||||
|
inequal $counter $calcsize &cond
|
||||||
|
if $cond %loopstart
|
||||||
|
# End loop
|
||||||
|
|
||||||
|
## Computing
|
||||||
|
|
||||||
|
# Convert types
|
||||||
|
stod $left &left
|
||||||
|
stod $right &right
|
||||||
|
|
||||||
|
# Calculations
|
||||||
|
# Adding
|
||||||
|
inequal $operator '+' &cond
|
||||||
|
if $cond %subtract
|
||||||
|
add $left $right &result
|
||||||
|
stdlnout $result
|
||||||
|
jump %begin
|
||||||
|
|
||||||
|
# Subtracting
|
||||||
|
@subtract
|
||||||
|
inequal $operator '-' &cond
|
||||||
|
if $cond %multiply
|
||||||
|
subtract $left $right &result
|
||||||
|
stdlnout $result
|
||||||
|
jump %begin
|
||||||
|
|
||||||
|
# Multiplying
|
||||||
|
@multiply
|
||||||
|
inequal $operator '*' &cond
|
||||||
|
if $cond %divide
|
||||||
|
multiply $left $right &result
|
||||||
|
stdlnout $result
|
||||||
|
jump %begin
|
||||||
|
|
||||||
|
# Dividing
|
||||||
|
@divide
|
||||||
|
inequal $operator '/' &cond
|
||||||
|
if $cond %error
|
||||||
|
divide $left $right &result
|
||||||
|
stdlnout $result
|
||||||
|
jump %begin
|
||||||
|
|
||||||
|
@error
|
||||||
|
stdlnout "Uh oh something terribly terrible happened lmao"
|
||||||
|
jump %begin
|
Reference in New Issue
Block a user