HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Fuzzy Octo Guacamole interpreter

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
interpreterguacamolefuzzyocto

Problem

This is my code that interprets my own golfing and esoteric language, Fuzzy-Octo-Guacamole.

It has 2 stacks to store information in, and various operations can be performed on each stack, or both at once.

This is also my first real project using classes, so any advice on how I did would be much appreciated.

```
#!/usr/bin/env python2
# -- coding: utf-8 --
"""
^ push
ಠ or . shift
_ pop
: get
o peek
; print
number push number
{ } if
( ) loop
@ exit
+ increment
  • decrement


? increment global counter
! set global counter to top of stack
' ' no-op
c clear active stack
" string literal
' string literal
U push 0
N push None
K print _UNK
I interleave
print goat ascii art
g print goat ascii art
= ala ruby rocketship op
'dsa'
* multiply the top 2 items on the active stack
"""

import re
import sys
import string
from stack import Stack

class FOG(object):

# Storage
commands = {
"^": "_input",# ^_^
"ಠ": "_swap_stacks", # ಠ_ಠ
".": "_swap_stacks", # ._.
"_": "_pop",
":": "_print_all",# :_:
"o": "_peek", # o_O
";": "_print_register",# ;_;
"{": "_skip",# {_{
"@": "_exit", # @_@
"+": "_increment",# +_+
"-": "_decrement",# -_-
"?": "_increment_register",# ?_?
"c": "_clear",# c_c
'"': "_string_lit",# "_"
"(": "_infinite_loop",# (_(
"[": "_for_loop",# [_[
"!": "_set_register",# !_!
"U": "_zero",# U_U
"N": "_none",# N_N
"K": "_unk",# K_K
"I": "_interleave",# I_I
")": "_noop",# )_)
"}": "_noop",# }_}
"]": "_noop",# ]_]
" ": "_noop",
"'": "_string_lit2",# '_'
">": "_noop",# >_>
"": "_goat",# _

Solution

I didn't got much into the language but there are a few things that doesn't seems quite right:

  • Documentation says {} is if, code says it is skip. As such u'0{g}' prints a goat and u'0i{g}' doesn't, it is weird for an if to be triggered by a falsey value instead of a truthy one. Choose one, fix the other.



  • Why bother encoding and decoding when all your symbols are in the commands dictionary?



-
_simplify is better written

def _simplify(self, val):
    try:
        return int(val)
    except ValueError:
        try:
            return float(val)
        except ValueError:
            return unicode(val)


Also it's usage seems weird as you simplify most of the stuff you push in your stacks and also simplify on pop. Since you can push lists using <>, I don't understand why you’d want to convert them to unicode before ! for instance.

  • How am I supposed to push 20 on top of the stack? u'20_;' prints 0. Do I really need to use u'45_;' to print 20? And for 111, is it really u'661a3*_;'? Try to not push unrecognized characters one at a time.



  • You need a parser. Indexes won't get you very far when it comes to nested structures: u'>' builds `[20, u'

Code Snippets

def _simplify(self, val):
    try:
        return int(val)
    except ValueError:
        try:
            return float(val)
        except ValueError:
            return unicode(val)

Context

StackExchange Code Review Q#124736, answer score: 5

Revisions (0)

No revisions yet.