From: Jean Privat Date: Fri, 19 Jun 2009 14:05:33 +0000 (-0400) Subject: syntax: better internal VariableContex stype handling X-Git-Tag: v0.3~245 X-Git-Url: http://nitlanguage.org?ds=sidebyside syntax: better internal VariableContex stype handling Remove the sublcass CastVariableContex. Add the method VariableContext::stype= Current client does not need to be updated. Signed-off-by: Jean Privat --- diff --git a/src/syntax/control_flow.nit b/src/syntax/control_flow.nit index ceadd4e..2eddf2c 100644 --- a/src/syntax/control_flow.nit +++ b/src/syntax/control_flow.nit @@ -66,7 +66,18 @@ abstract class VariableContext # May be different from the declaration static type meth stype(v: Variable): MMType do - return v.stype + if _stypes.has_key(v) then + return _stypes[v] + else + return v.stype + end + end + + # Set effective static type of a given variable + # May be different from the declaration static type + meth stype=(v: Variable, t: MMType) + do + _stypes[v] = t end # Variables by name (in the current context only) @@ -75,6 +86,9 @@ abstract class VariableContext # All variables in all contextes attr _all_variables: Set[Variable] + # Updated static type of variables + attr _stypes: Map[Variable, MMType] = new HashMap[Variable, MMType] + # Build a new VariableContext meth sub(node: PNode): SubVariableContext do @@ -84,7 +98,9 @@ abstract class VariableContext # Build a nested VariableContext with new variable information meth sub_with(node: PNode, v: Variable, t: MMType): SubVariableContext do - return new CastVariableContext.with_prev(self, node, v, t) + var ctx = sub(node) + ctx.stype(v) = t + return ctx end # The visitor of the context (used to display error) @@ -171,7 +187,11 @@ special VariableContext redef meth stype(v) do - return prev.stype(v) + if _stypes.has_key(v) then + return _stypes[v] + else + return prev.stype(v) + end end init with_prev(p: VariableContext, node: PNode) @@ -187,27 +207,6 @@ special VariableContext end end -class CastVariableContext -special SubVariableContext - attr _variable: Variable - attr _var_type: MMType - - redef meth stype(v) - do - if _variable == v then - return _var_type - end - return prev.stype(v) - end - - init with_prev(p: VariableContext, node: PNode, v: Variable, t: MMType) - do - super(p, node) - _variable = v - _var_type =t - end -end - redef class Variable # Is the variable must be set before being used ? meth must_be_set: Bool do return false