Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added new object expression

  • Loading branch information...
commit 79d839054ebcf671080d2ec4677093c9d77dcf89 1 parent 9f31841
@nardo authored
View
22 interpreter/kt_compiler.py
@@ -901,4 +901,24 @@ def _analyze_expr_map_expr(self, si, expr, valid_types, is_lvalue):
self.analyze_expression(si, pair['value'], ('any'), False)
def _compile_expr_map_expr(self, si, expr, valid_types, is_lvalue):
- return ('map', [(self.compile_expression(si, pair['key'], ('any'), False), self.compile_expression(si, pair['value'], ('any'), False)) for pair in expr['map_pairs']])
+ return ('map', [(self.compile_expression(si, pair['key'], ('any'), False), self.compile_expression(si, pair['value'], ('any'), False)) for pair in expr['map_pairs']])
+
+ def _analyze_expr_new_object_expr(self, si, expr, valid_types, is_lvalue):
+ if is_lvalue:
+ raise compile_error, (expr, "new expression cannot be an lvalue.")
+ # search the global container
+ node = self.find_node(si.compound_node, expr['parent_name'])
+ if not node:
+ raise compile_error, (expr, "locator " + locator_name + " not found.")
+ expr['location'] = ('global_node', node)
+ if not (node.type == 'class' or node.type == 'struct'):
+ raise compile_error, (expr, "new only allowed for classes and structs.")
+ for arg in expr['argument_expr_list']:
+ self.analyze_expression(si, arg, ('any'), False)
+
+ def _compile_expr_new_object_expr(self, si, expr, valid_types, is_lvalue):
+ return ('new', expr['location'], [self.compile_expression(si, arg, ('any'), False) for arg in expr['argument_expr_list']])
+
+
+
+
View
30 interpreter/kt_vm.py
@@ -29,7 +29,10 @@ def eval(self, vm):
return self
class class_instance:
def __init__(self, node):
- pass
+ self.node = node
+ self.compound_record = node.compound_record
+ def eval(self, vm):
+ return self
class function_instance:
def __init__(self, node):
self.func_record = node.func_record
@@ -316,8 +319,23 @@ def _eval_map(self, map_pairs_list):
value = self.eval(pair[1])
result[key] = value
return result
-
-# because I'm an optimization nerd.
+ #new_object_expr
+ # parent_name
+ # argument_expr_list
+
+ def _eval_new(self, class_node, args):
+ cnode = self.eval(class_node)
+ result = vm.object_instance(cnode.node)
+ evaluated_args = [self.eval(arg) for arg in args]
+ if cnode.node.constructor_index is not None:
+ callable = ('func_rec', self.image.functions[cnode.node.constructor_index], result)
+ self.call_function(callable, evaluated_args)
+ return result
+
+
+ #new_object_expr_type_expr
+ # parent_name_expr
+ # argument_expr_list
#('prev_scope', ()) - a reference to a variable in a previous scope
@@ -412,9 +430,3 @@ def _eval_map(self, map_pairs_list):
#function_expr
# parameter_list
# expr
- #new_object_expr
- # parent_name
- # argument_expr_list
- #new_object_expr_type_expr
- # parent_name_expr
- # argument_expr_list
View
4 parser/parser.bison
@@ -1237,9 +1237,9 @@ new_expression
optional_argument_specifier
:
- { $$ = nil; }
+ { $$ = empty_list(); }
| '(' ')'
- { $$ = nil; }
+ { $$ = empty_list(); }
| '(' argument_expression_list ')'
{ $$ = $2; }
;
View
4 test/test3/inheritance_test.kt
@@ -36,4 +36,6 @@ function main()
the_object.do_print()
'container/sub_object2'.do_print()
sub_object2.do_print()
- 'container2/sub_object2'.do_print()
+ 'container2/sub_object2'.do_print()
+ var object3 = new test_subclass(100, 200)
+ object3.do_print()
Please sign in to comment.
Something went wrong with that request. Please try again.