From b277a638393ce3d2dd24f5bc013ea3adfe0d3bca Mon Sep 17 00:00:00 2001 From: Jean Privat Date: Mon, 19 Aug 2024 15:53:11 -0400 Subject: [PATCH] nitc: two example of made-up program, one by a string and one by the astbuilder Signed-off-by: Jean Privat --- src/examples/module_fictive.nit | 61 ++++++++++++++++++++++++++++++++ src/examples/module_injected.nit | 42 ++++++++++++++++++++++ tests/nitcg.skip | 2 ++ tests/niti.skip | 2 ++ tests/sav/module_fictive.res | 11 ++++++ tests/sav/module_injected.res | 14 ++++++++ 6 files changed, 132 insertions(+) create mode 100644 src/examples/module_fictive.nit create mode 100644 src/examples/module_injected.nit create mode 100644 tests/sav/module_fictive.res create mode 100644 tests/sav/module_injected.res diff --git a/src/examples/module_fictive.nit b/src/examples/module_fictive.nit new file mode 100644 index 0000000000..39c8c45e83 --- /dev/null +++ b/src/examples/module_fictive.nit @@ -0,0 +1,61 @@ +# This file is part of NIT ( http://www.nitlanguage.org ). +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Sample program that show how to build (and run) a synthetic program from the AST builder +import nitc::astprinter +import nitc::interpreter +import nitc::astbuilder + +# 3 god classes +var tc = new ToolContext +tc.keep_going = true +var model = new Model +var mb = new ModelBuilder(model, tc) + +# load the code module. While implicit in source code, we need to retrieve it for synthetic code +var acore = mb.load_module("core") +var mcore = acore.mmodule +assert mcore != null +# Loading does not perform the analysis, so force the analysis now +mb.run_phases + +# A synthetic main module +var mmodule = new MModule(model, null, "m0", model.no_location) +mmodule.set_imported_mmodules([mcore]) # Close the module hierarchy +mmodule.set_visibility_for(mcore, public_visibility) # Important, or else `_name` methods will fail + +# Refine Sys in the main module +var mclass = mmodule.sys_type +assert mclass != null +var mclassdef = new MClassDef(mmodule, mclass, model.no_location) +mclassdef.add_in_hierarchy # Close the classdef hierarchy + +# Redefine Sys::main +var nmeth = mb.create_method_from_name("main", mclassdef, false) + +# Body is `self.print(42)` +var ab = new ASTBuilder(mmodule, mclass) +var narg = ab.make_int(42) +var nrecv = ab.make_self +var mprint = mb.try_get_mproperty_by_name2(null, mmodule, mclass, "print") +assert mprint isa MMethod +var callsite = ab.create_callsite(mprint, false) +var ncall = ab.make_call(nrecv, callsite, [narg]) +nmeth.n_block.add(ncall) + +nmeth.dump_tree(false, false) +ncall.print_tree + +var interpreter = new NaiveInterpreter(mb, mmodule, new Array[String]) +interpreter.start(mmodule) diff --git a/src/examples/module_injected.nit b/src/examples/module_injected.nit new file mode 100644 index 0000000000..2bb66cabf4 --- /dev/null +++ b/src/examples/module_injected.nit @@ -0,0 +1,42 @@ +# This file is part of NIT ( http://www.nitlanguage.org ). +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Sample program that show how to load (and run) a program from a String +module module_injected + +import nitc::parser_util +import nitc::astprinter +import nitc::interpreter + +# 3 god classes +var tc = new ToolContext +tc.keep_going = true +var model = new Model +var mb = new ModelBuilder(model, tc) + +# Parse the module +var amodule = tc.parse_module("print 42") +# Add it to the model through the model builder +var mmodule = mb.load_rt_module(null, amodule, "-") +assert mmodule != null +# Do the semantic analysis +mb.run_phases + +amodule.dump_tree +var main_method = amodule.n_classdefs.last.n_propdefs.first +main_method.print_tree +assert main_method isa AMethPropdef + +var interpreter = new NaiveInterpreter(mb, mmodule, new Array[String]) +interpreter.start(mmodule) diff --git a/tests/nitcg.skip b/tests/nitcg.skip index 3c378aa029..e61d597669 100644 --- a/tests/nitcg.skip +++ b/tests/nitcg.skip @@ -11,3 +11,5 @@ get_mclasses ^nit test_astbuilder test_astprinter +module_fictive +module_injected diff --git a/tests/niti.skip b/tests/niti.skip index 551351b0d6..340b280e0b 100644 --- a/tests/niti.skip +++ b/tests/niti.skip @@ -48,3 +48,5 @@ nitpm nitdoc test_astbuilder test_astprinter +module_fictive +module_injected diff --git a/tests/sav/module_fictive.res b/tests/sav/module_fictive.res new file mode 100644 index 0000000000..8edc936ff3 --- /dev/null +++ b/tests/sav/module_fictive.res @@ -0,0 +1,11 @@ +AMethPropdef @ +|--APublicVisibility +|--AIdMethid +|--ASignature +`--ABlockExpr + `--ACallExpr call=Sys.file$Sys$print(object: Object) + |--ASelfExpr + `--AListExprs + `--AIntegerExpr :Int +self.print(42) +42 diff --git a/tests/sav/module_injected.res b/tests/sav/module_injected.res new file mode 100644 index 0000000000..bc55b36d31 --- /dev/null +++ b/tests/sav/module_injected.res @@ -0,0 +1,14 @@ +1 print 42 +AModule @1,1--8 +`--AMainClassdef @1,1--8 + `--AMainMethPropdef @1,1--8 + `--ABlockExpr @1,1--8 + `--ACallExpr call=Sys.file$Sys$print(object: Object) @1,1--8 + |--AImplicitSelfExpr :Sys @1,1 + |--AQid @1,1--5 + | `--TId print @1,1--5 + `--AListExprs @1,7--8 + `--AIntegerExpr :Int @1,7--8 + `--TInteger 42 @1,7--8 +print(42) +42