From: Jean Privat Date: Fri, 26 Feb 2016 21:01:29 +0000 (-0500) Subject: engine: handle new attribute annotation `is_optional` X-Git-Url: http://nitlanguage.org engine: handle new attribute annotation `is_optional` Signed-off-by: Jean Privat --- diff --git a/src/compiler/abstract_compiler.nit b/src/compiler/abstract_compiler.nit index 3cba054..57c0fca 100644 --- a/src/compiler/abstract_compiler.nit +++ b/src/compiler/abstract_compiler.nit @@ -3092,7 +3092,18 @@ redef class AAttrPropdef v.assign(v.frame.returnvar.as(not null), res) else if mpropdef == mwritepropdef then assert arguments.length == 2 - v.write_attribute(self.mpropdef.mproperty, arguments.first, arguments[1]) + var recv = arguments.first + var arg = arguments[1] + if is_optional then + var value = v.new_var(self.mpropdef.static_mtype.as(not null)) + v.add("if ({arg} == NULL) \{") + v.assign(value, evaluate_expr(v, recv)) + v.add("\} else \{") + v.assign(value, arg) + v.add("\}") + arg = value + end + v.write_attribute(self.mpropdef.mproperty, arguments.first, arg) if is_lazy then var ret = self.mtype var useiset = not ret.is_c_primitive and not ret isa MNullableType diff --git a/src/interpreter/naive_interpreter.nit b/src/interpreter/naive_interpreter.nit index bbe07d1..23d3ba1 100644 --- a/src/interpreter/naive_interpreter.nit +++ b/src/interpreter/naive_interpreter.nit @@ -1497,7 +1497,12 @@ redef class AAttrPropdef return evaluate_expr(v, recv, f) else if mpropdef == mwritepropdef then assert args.length == 2 - v.write_attribute(attr, recv, args[1]) + var arg = args[1] + if is_optional and arg.mtype isa MNullType then + var f = v.new_frame(self, mpropdef, args) + arg = evaluate_expr(v, recv, f) + end + v.write_attribute(attr, recv, arg) return null else abort