diff options
author | Suleyman Farajli <suleyman@farajli.net> | 2024-12-18 17:06:47 +0400 |
---|---|---|
committer | Suleyman Farajli <suleyman@farajli.net> | 2024-12-18 17:06:47 +0400 |
commit | 0b4330fdf40707acafb33aad49dea49445a63b7a (patch) | |
tree | a0b7b0829a5406acfe337602f428eae8cb3e1f4c /src/main.go | |
parent | 86b601a16619189189e70048c4dd1d97f5d4a7c2 (diff) |
equal and if statement added
Diffstat (limited to 'src/main.go')
-rw-r--r-- | src/main.go | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/main.go b/src/main.go index f831ec8..7947e45 100644 --- a/src/main.go +++ b/src/main.go @@ -33,9 +33,14 @@ func interpret(raw[]string) { var err error; var number int; var stack[]int; + var skip_to_end bool; for i := 0; i < len(raw); i++ { length := len(stack); + /* Conditional looping */ + if (skip_to_end) { + continue; + } number, err = strconv.Atoi(raw[i]); if err == nil { stack = append(stack, number); @@ -55,12 +60,32 @@ func interpret(raw[]string) { } stack[length - 2] = stack[length - 1] - stack[length - 2] stack = stack[:length - 1]; + case "=": + if length < 2 { + panic("failed to check equality: stack underflow"); + } + if (stack[length - 1] == stack[length - 2]) { + stack = append(stack, -1); + } else { + stack = append(stack, 0); + } + case ".": if length < 1 { panic("failed to dump: stack underflow"); } fmt.Println(stack[length - 1]); stack = stack[:length - 1]; + case "if": + if length < 1 { + panic("failed to if: stack underflow"); + } + if (stack[length - 1] == 0) { + skip_to_end = true; + } + case "end": + skip_to_end = false; + default: panic("invalid word"); } |