From a1ad1c52eb23bd782e1f6762b0711080596efa2d Mon Sep 17 00:00:00 2001 From: Jean Privat Date: Wed, 29 Oct 2014 14:24:39 -0400 Subject: [PATCH] tools: accept statement block in attributes Signed-off-by: Jean Privat --- src/compiler/abstract_compiler.nit | 9 +++++++++ src/interpreter/naive_interpreter.nit | 23 ++++++++++++++++++----- src/rapid_type_analysis.nit | 7 ++++--- src/semantize/typing.nit | 14 +++++++++++++- 4 files changed, 44 insertions(+), 9 deletions(-) diff --git a/src/compiler/abstract_compiler.nit b/src/compiler/abstract_compiler.nit index 94d5ada..b5052cb 100644 --- a/src/compiler/abstract_compiler.nit +++ b/src/compiler/abstract_compiler.nit @@ -2351,8 +2351,17 @@ redef class AAttrPropdef assert mtype != null var nexpr = self.n_expr + var nblock = self.n_block if nexpr != null then value = v.expr(nexpr, mtype) + else if nblock != null then + value = v.new_var(mtype) + frame.returnvar = value + frame.returnlabel = v.get_name("RET_LABEL") + v.add("\{") + v.stmt(nblock) + v.add("{frame.returnlabel.as(not null)}:(void)0;") + v.add("\}") else abort end diff --git a/src/interpreter/naive_interpreter.nit b/src/interpreter/naive_interpreter.nit index 12a54de..e358b12 100644 --- a/src/interpreter/naive_interpreter.nit +++ b/src/interpreter/naive_interpreter.nit @@ -1076,8 +1076,7 @@ redef class AAttrPropdef private fun init_expr(v: NaiveInterpreter, recv: Instance) do if is_lazy then return - var nexpr = self.n_expr - if nexpr != null then + if has_value then evaluate_expr(v, recv) return end @@ -1091,12 +1090,26 @@ redef class AAttrPropdef private fun evaluate_expr(v: NaiveInterpreter, recv: Instance): Instance do assert recv isa MutableInstance - var nexpr = self.n_expr - assert nexpr != null var f = new Frame(self, self.mpropdef.as(not null), [recv]) v.frames.unshift(f) - var val = v.expr(nexpr) + + var val + + var nexpr = self.n_expr + var nblock = self.n_block + if nexpr != null then + val = v.expr(nexpr) + else if nblock != null then + v.stmt(nblock) + assert v.returnmark == f + val = v.escapevalue + v.returnmark = null + v.escapevalue = null + else + abort + end assert val != null + v.frames.shift assert not v.is_escaping v.write_attribute(self.mpropdef.mproperty, recv, val) diff --git a/src/rapid_type_analysis.nit b/src/rapid_type_analysis.nit index e5e4438..378c31d 100644 --- a/src/rapid_type_analysis.nit +++ b/src/rapid_type_analysis.nit @@ -349,11 +349,12 @@ class RapidTypeAnalysis var nclassdef = self.modelbuilder.mclassdef2nclassdef[cd] for npropdef in nclassdef.n_propdefs do if not npropdef isa AAttrPropdef then continue - var nexpr = npropdef.n_expr - if nexpr == null then continue + if not npropdef.has_value then continue + var mpropdef = npropdef.mpropdef.as(not null) var v = new RapidTypeVisitor(self, bound_mtype, mpropdef) - v.enter_visit(nexpr) + v.enter_visit(npropdef.n_expr) + v.enter_visit(npropdef.n_block) end end diff --git a/src/semantize/typing.nit b/src/semantize/typing.nit index 67021b7..1fb3e07 100644 --- a/src/semantize/typing.nit +++ b/src/semantize/typing.nit @@ -602,6 +602,10 @@ redef class AAttrPropdef var mtype = self.mpropdef.static_mtype v.visit_expr_subtype(nexpr, mtype) end + var nblock = self.n_block + if nblock != null then + v.visit_stmt(nblock) + end end end @@ -813,7 +817,15 @@ redef class AReturnExpr redef fun accept_typing(v) do var nexpr = self.n_expr - var ret_type = v.mpropdef.as(MMethodDef).msignature.return_mtype + var ret_type + var mpropdef = v.mpropdef + if mpropdef isa MMethodDef then + ret_type = mpropdef.msignature.return_mtype + else if mpropdef isa MAttributeDef then + ret_type = mpropdef.static_mtype + else + abort + end if nexpr != null then if ret_type != null then v.visit_expr_subtype(nexpr, ret_type) -- 1.7.9.5