Skip to content
Browse files

Added curly syntax, added `if` sample.

  • Loading branch information...
1 parent efcd35d commit 86a464ef38dbbfdb1c8a633b5529f321842460e2 @fredreichbier committed
Showing with 54 additions and 13 deletions.
  1. +5 −0 edleks/edleks/ast.py
  2. +18 −4 edleks/edleks/compiler.py
  3. +15 −9 edleks/edleks/parser.py
  4. +16 −0 edleks/tests/if.ed
View
5 edleks/edleks/ast.py
@@ -60,6 +60,11 @@ def __init__(self, coord, op, left, right):
self.left = left
self.right = right
+class Arrow(Node):
+ def __init__(self, coord, elems):
+ Node.__init__(self, coord)
+ self.elems = elems
+
class Visitor(object):
def visit(self, node):
return self.dispatch(node)
View
22 edleks/edleks/compiler.py
@@ -1,9 +1,20 @@
from . import ast, skelde
+def flatten(l):
+ for e in l:
+ if isinstance(e, (list, tuple)):
+ for ee in e:
+ yield ee
+ else:
+ yield e
+
+def flattenl(l):
+ return list(flatten(l))
+
class CompilingVisitor(ast.Visitor):
def visit_Message(self, node):
previous = self.visit(node.left) if node.left else None
- arguments = map(self.visit, node.arguments)
+ arguments = flattenl(map(self.visit, node.arguments))
return skelde.Message(node.name, arguments, previous)
def visit_String(self, node):
@@ -13,7 +24,10 @@ def visit_Int(self, node):
return skelde.Message(node.value)
def visit_BinaryOp(self, node):
- return self.visit(node.left)(node.op, [self.visit(node.right)])
+ return self.visit(node.left)(node.op, flattenl([self.visit(node.right)]))
+
+ def visit_Arrow(self, node):
+ return map(self.visit, node.elems)
def visit_Block(self, node):
last_sk_message = None
@@ -35,11 +49,11 @@ def visit_Block(self, node):
def visit_SetSlot(self, node):
left = self.visit(node.left) if node.left is not None else None
return skelde.Message('set_slot',
- [skelde.Message('"%s"' % node.name), self.visit(node.value)],
+ flattenl([skelde.Message('"%s"' % node.name), self.visit(node.value)]),
left)
def visit_UpdateSlot(self, node):
left = self.visit(node.left) if node.left is not None else None
return skelde.Message('update_slot',
- [skelde.Message('"%s"' % node.name), self.visit(node.value)],
+ flattenl([skelde.Message('"%s"' % node.name), self.visit(node.value)]),
left)
View
24 edleks/edleks/parser.py
@@ -13,7 +13,7 @@ def d_expression(t):
''' expression: set_slot
| update_slot
| paren
- | if
+ | arrow
| message
'''
return t[0]
@@ -38,9 +38,9 @@ def d_NL(t):
r''' NL: "[\n]*" '''
pass
-def d_if(t, nodes):
- r''' if: '%if' expression arguments '''
- print t, nodes
+def d_arrow(t, nodes):
+ r''' arrow: '{' arguments_really '->' (NL*) arguments_really '}' '''
+ return make_node(nodes, ast.Arrow, t[1] + t[4])
def d_set_slot(t, nodes):
r''' set_slot: expression? identifier ':=' expression '''
@@ -70,16 +70,22 @@ def d_message_send(t, nodes):
return make_node(nodes, ast.Message, left, name, arguments)
def d_paren(t):
- ''' paren: '(' expression ')' '''
+ ''' paren: '(' expressions ')'
+ | '(' expression ')'
+ '''
return t[1]
def d_arguments(t):
- ''' arguments: '(' (expressions (',' expressions)*)? ')' '''
+ ''' arguments: '(' arguments_really? ')' '''
if t[1]:
- return [t[1][0][0]] + map(lambda x: x[1], t[1][0][1:])
+ return t[1][0]
else:
return []
+def d_arguments_really(t):
+ ''' arguments_really: expressions (',' expressions)* '''
+ return [t[0]] + map(lambda x: x[1], t[1])
+
def d_binary_op(t, nodes):
''' binary_op: expression bop expression '''
return make_node(nodes, ast.BinaryOp, t[1], t[0], t[2])
@@ -93,7 +99,7 @@ def d_bop(t):
def d_identifier(t):
# r''' identifier: "[a-zA-Z_][a-zA-Z0-9_]*" ''' # kind of stupid!
- r''' identifier: "[^0-9 ;()\":=\r\n,][^ ;()\":=\r\n,]*" ''' # kind of complicated!
+ r''' identifier: "[^0-9 ;()\":=\r{}\n,][^ ;()\":=\r\n{},]*" ''' # kind of complicated!
return t[0]
def d_string(t, nodes):
@@ -106,4 +112,4 @@ def d_int(t, nodes): # TODO: float
def parse(s):
parser = Parser()
- return parser.parse(s).getStructure()
+ return parser.parse(s, print_debug_info=0).getStructure()
View
16 edleks/tests/if.ed
@@ -0,0 +1,16 @@
+test := Method clone({number ->
+ if({number == 123 ->
+ "One two three hihihihi" println},
+ {number == 456 ->
+ "Four five six ixixixix" println},
+ "Dunno." println
+ )
+})
+
+moo := Method clone({a, b -> a + b})
+
+test(moo(122, 1))
+test(228 + 228)
+test(1)
+
+

0 comments on commit 86a464e

Please sign in to comment.
Something went wrong with that request. Please try again.