Skip to content

Commit 22fd41c

Browse files
committed
Merge: src/astbuilder: Add new nodes makers
Add new make method for three kind of nodes (AAttrPropdef, ACallAssignExpr) Pull-Request: #2810 Reviewed-by: Jean Privat <[email protected]>
2 parents c99623f + 1d5851b commit 22fd41c

File tree

1 file changed

+114
-7
lines changed

1 file changed

+114
-7
lines changed

src/astbuilder.nit

+114-7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ intrude import literal
2020
intrude import parser
2121
intrude import semantize::scope
2222
intrude import modelbuilder_base
23+
intrude import modelize_property
2324

2425
# General factory to build semantic nodes in the AST of expressions
2526
class ASTBuilder
@@ -165,6 +166,41 @@ class ASTBuilder
165166
return new ANotExpr.make(expr)
166167
end
167168

169+
# Make a new attribute
170+
fun make_attribute(name: String,
171+
n_type: nullable AType,
172+
n_visibility: nullable AVisibility,
173+
initial_value: nullable AExpr,
174+
n_block: nullable AExpr,
175+
m_attributedef: nullable MAttributeDef,
176+
m_setterdef: nullable MMethodDef,
177+
m_getterdef: nullable MMethodDef): AAttrPropdef
178+
do
179+
return new AAttrPropdef.make(name, n_type, n_visibility, initial_value, n_block, m_attributedef, m_setterdef, m_getterdef)
180+
end
181+
182+
# Make a new class (AStdClassdef)
183+
fun make_class(mclassdef: nullable MClassDef,
184+
n_visibility: nullable AVisibility,
185+
n_formaldefs : Collection[AFormaldef],
186+
n_extern_code_block : nullable AExternCodeBlock,
187+
n_propdefs : Collection[APropdef],
188+
n_classkind: nullable AClasskind): AStdClassdef
189+
do
190+
return new AStdClassdef.make(mclassdef, n_visibility, n_formaldefs, n_extern_code_block, n_propdefs, n_classkind)
191+
end
192+
193+
fun make_var(variable: Variable, mtype: MType): AVarExpr
194+
do
195+
return new AVarExpr.make(variable, mtype)
196+
end
197+
198+
# Make a call assignment i.e `a = 10`
199+
fun make_call_assign(recv: AExpr, callsite: CallSite, n_args: nullable Collection[AExpr], n_value: AExpr): ACallAssignExpr
200+
do
201+
return new ACallAssignExpr.make(recv, callsite, n_args, n_value)
202+
end
203+
168204
# Build a callsite to call the `mproperty` in the current method `caller_method`.
169205
# `is_self_call` indicate if the method caller is a property of `self`
170206
fun create_callsite(modelbuilder: ModelBuilder, caller_method : AMethPropdef, mproperty: MMethod, is_self_call: Bool): CallSite
@@ -262,6 +298,68 @@ class APlaceholderExpr
262298
end
263299
end
264300

301+
redef class ACallAssignExpr
302+
private init make(recv: AExpr, callsite: CallSite, args: nullable Collection[AExpr], n_value: AExpr)
303+
do
304+
_callsite = callsite
305+
_mtype = callsite.recv
306+
_is_typed = true
307+
var n_args = new AListExprs
308+
if args != null then
309+
n_args.n_exprs.add_all(args)
310+
end
311+
var n_qid = new AQid
312+
n_qid.n_id = new TId
313+
n_qid.n_id.text = callsite.mproperty.name
314+
init_acallassignexpr(recv, n_qid, n_args, new TAssign, n_value)
315+
end
316+
end
317+
318+
redef class AStdClassdef
319+
private init make(mclassdef: nullable MClassDef,
320+
n_visibility: nullable AVisibility,
321+
n_formaldefs : Collection[Object],
322+
n_extern_code_block : nullable AExternCodeBlock,
323+
n_propdefs : Collection[Object],
324+
n_classkind: nullable AClasskind)
325+
do
326+
if n_visibility == null then n_visibility = new APublicVisibility
327+
if n_classkind == null then n_classkind = new AConcreteClasskind.init_aconcreteclasskind(new TKwclass)
328+
var n_qid = new AQclassid.init_aqclassid(null, new TClassid)
329+
init_astdclassdef(null, null, n_visibility, n_classkind, n_qid, null, n_formaldefs, null, n_extern_code_block, n_propdefs, new TKwend)
330+
_mclassdef = mclassdef
331+
_mclass = mclassdef.mclass
332+
end
333+
end
334+
335+
redef class AAttrPropdef
336+
337+
# Create a new `AAttrPropdef`
338+
# Note: By default if the `AVisibility` is not given the visibility is set to public
339+
private init make(name: String,
340+
n_type: nullable AType,
341+
n_visibility: nullable AVisibility,
342+
initial_value: nullable AExpr,
343+
n_block: nullable AExpr,
344+
m_attributedef: nullable MAttributeDef,
345+
m_setterdef: nullable MMethodDef,
346+
m_getterdef: nullable MMethodDef)
347+
do
348+
# Set the model type
349+
if n_type != null then mtype = n_type.mtype
350+
# Define the visibility default is public
351+
if n_visibility == null then n_visibility = new APublicVisibility
352+
init_aattrpropdef(null, null, n_visibility, new TKwvar, new TId, n_type, null, initial_value, null, null , n_block, null)
353+
# Set the name of the attribute
354+
_n_id2.text = name
355+
_mpropdef = m_attributedef
356+
_mreadpropdef = m_getterdef
357+
_mwritepropdef = m_setterdef
358+
if initial_value != null or n_block != null then has_value = true
359+
if m_attributedef != null then self.location = m_attributedef.location
360+
end
361+
end
362+
265363
redef class ANotExpr
266364
private init make(expr: AExpr)
267365
do
@@ -318,6 +416,7 @@ redef class AMethPropdef
318416
if n_visibility == null then n_visibility = new APublicVisibility
319417
self.init_amethpropdef(null,tk_redef,n_visibility,new TKwmeth,null,null,null,n_methid,n_signature,n_annotations,n_extern_calls,n_extern_code_block,new TKwdo,n_block,new TKwend)
320418
self.mpropdef = mmethoddef
419+
if mpropdef != null then self.location = mmethoddef.location
321420
end
322421
end
323422

@@ -538,13 +637,6 @@ redef class ACallExpr
538637
end
539638
end
540639

541-
redef class AAsCastExpr
542-
private init make(n_expr: AExpr, n_type: AType)
543-
do
544-
init_aascastexpr(n_expr, new TKwas , null , n_type, null)
545-
end
546-
end
547-
548640
redef class AAsNotnullExpr
549641
private init make(n_expr: AExpr, t: nullable MType)
550642
do
@@ -642,6 +734,20 @@ redef class AVarAssignExpr
642734
end
643735

644736
redef class ASignature
737+
738+
init make_from_msignature(msignature: MSignature)
739+
do
740+
var nparams = new Array[AParam]
741+
for mparam in msignature.mparameters do
742+
var variable = new Variable(mparam.name)
743+
variable.declared_type = mparam.mtype
744+
n_params.add(new AParam.make(variable, new AType.make(mparam.mtype)))
745+
end
746+
var return_type = null
747+
if msignature.return_mtype != null then return_type = new AType.make(msignature.return_mtype)
748+
init_asignature(null, nparams, null, return_type)
749+
end
750+
645751
redef fun clone: SELF
646752
do
647753
var ntype = n_type
@@ -655,6 +761,7 @@ redef class AParam
655761
private init make(v: nullable Variable, t: nullable AType)
656762
do
657763
_n_id = new TId
764+
if v != null then _n_id.text = v.name
658765
_variable = v
659766
_n_type = t
660767
end

0 commit comments

Comments
 (0)