Skip to content

Commit

Permalink
changed the parsing of one-character functions
Browse files Browse the repository at this point in the history
implemented the basic comparison functions
  • Loading branch information
Swizec committed Aug 28, 2010
1 parent 9ec1e84 commit e333b99
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 12 deletions.
18 changes: 9 additions & 9 deletions compiler/compile.py
Expand Up @@ -3,7 +3,10 @@

import parsers

TRANSLATIONS = {'.': 'dot'}
TRANSLATIONS = {'.': 'dot',
'=': 'equals',
'>': 'greater_than',
'<': 'less_than'}

def parse(code):
def _string(code):
Expand All @@ -14,14 +17,14 @@ def _string(code):
return (' '.join(s), len(s))
raise ParsingError("Unterminated string")

def _consume(code, n):
def _consume(code, n, depth=0):
stack = []

if n == 'Inf':
n = len(code)

def _parse(i):
(args, offset) = _consume(code[i+1:], parser.N_ARGS)
(args, offset) = _consume(code[i+1:], parser.N_ARGS, depth+1)
i += offset
element = parser.main(args)
return (element, i)
Expand All @@ -32,15 +35,11 @@ def _parse(i):

if element == '':
return (stack, i)

if element[0] == '"':
(element, offset) = _string(code[i:])
i += offset-1

if element[-1:] in TRANSLATIONS.keys():
stack.append(element[:-1])
element = element[-1:]

try:
parser = getattr(parsers, element)
except AttributeError:
Expand Down Expand Up @@ -71,14 +70,15 @@ def compile(source, target=None):
f = file(source, 'r')

code = reduce(lambda a,b: a+b.strip().split(' ') if b[0] != '#' else a, f, [])
code = reduce(lambda a,b: a+[b[:-1],b[-1:]] if b[-1:] in TRANSLATIONS.keys() and len(b)>1 else a+[b],
code, [])
result = parse(code)

f.close()

if target == None:
target = '/'.join([os.getcwd(), '.'.join([source.rsplit('/', 1)[1], 'py'])])


save(result, target)

def save(result, target):
Expand Down
6 changes: 6 additions & 0 deletions compiler/parsers/equals.py
@@ -0,0 +1,6 @@

N_ARGS = 2

def main(args):
(arg1, arg2) = args
return '(%s == %s)' % (arg1, arg2)
6 changes: 6 additions & 0 deletions compiler/parsers/greater_than.py
@@ -0,0 +1,6 @@

N_ARGS = 2

def main(args):
(arg1, arg2) = args
return '(%s > %s)' % (arg1, arg2)
2 changes: 1 addition & 1 deletion compiler/parsers/is.py
Expand Up @@ -2,4 +2,4 @@
N_ARGS = 1

def main(arg):
return 'True if %s else False' % arg
return 'True if %s else False' % arg[0]
5 changes: 5 additions & 0 deletions compiler/parsers/isnt.py
@@ -0,0 +1,5 @@

N_ARGS = 1

def main(arg):
return 'False if %s else True' % arg[0]
6 changes: 6 additions & 0 deletions compiler/parsers/less_than.py
@@ -0,0 +1,6 @@

N_ARGS = 2

def main(args):
(arg1, arg2) = args
return '(%s < %s)' % (arg1, arg2)
27 changes: 25 additions & 2 deletions examples/is.mur
@@ -1,4 +1,27 @@

say is "meow" " " "hai"
# prints True
say is "meow"

say "meow" " " "mrow"
# prints False
say is 0

# prints True
say is= "meow" "meow"

# prints True
say is> 1 0

# prints True
say is< 0 1

# prints False
say is= "meo" "meow"

# prints False
say is> 0 1

# prints False
say is< 1 0

# of course this also works
say > 1 0
24 changes: 24 additions & 0 deletions examples/isnt.mur
@@ -0,0 +1,24 @@

# prints False
say isnt "meow"

# prints True
say isnt 0

# prints False
say isnt= "meow" "meow"

# prints False
say isnt> 1 0

# prints False
say isnt< 0 1

# prints True
say isnt= "meo" "meow"

# prints True
say isnt> 0 1

# prints True
say isnt< 1 0

0 comments on commit e333b99

Please sign in to comment.