syntax: if absent, create classes Object and Bool in root module
authorJean Privat <jean@pryen.org>
Tue, 7 Jul 2009 20:34:24 +0000 (16:34 -0400)
committerJean Privat <jean@pryen.org>
Tue, 7 Jul 2009 20:34:24 +0000 (16:34 -0400)
Signed-off-by: Jean Privat <jean@pryen.org>

src/syntax/mmbuilder.nit
src/syntax/syntax_base.nit
tests/base_no_object.nit [new file with mode: 0644]
tests/sav/base_no_object.sav [new file with mode: 0644]
tests/sav/base_no_object_alt1.sav [new file with mode: 0644]

index de5ccbe..3944f03 100644 (file)
@@ -45,6 +45,10 @@ redef class MMSrcModule
                mmbv.visit(node)
                if tc.error_count > 0 then exit(1)
 
+               if mhe.direct_greaters.is_empty then
+                       process_default_classes(tc)
+               end
+
                # Import unrefined local classes and attach them to global classes
                import_local_classes
 
@@ -120,6 +124,23 @@ redef class MMSrcModule
 
                if tc.error_count > 0 then exit(1)
        end
+
+       # Create some primitive default classes if they do not exists
+       fun process_default_classes(tc: ToolContext)
+       do
+               var name = once ("Object".to_symbol)
+               if not has_global_class_named(name) then
+                       var c = new MMSrcLocalClass(self, name, null, 0)
+                       c.new_global
+                       src_local_classes[name] = c
+               end
+               name = once ("Bool".to_symbol)
+               if not has_global_class_named(name) then
+                       var c = new MMSrcLocalClass(self, name, null, 0)
+                       c.new_global
+                       src_local_classes[name] = c
+               end
+       end
 end
 
 redef class MMLocalClass
index 78cf92a..881e6aa 100644 (file)
@@ -68,10 +68,14 @@ special MMConcreteClass
        # Concrete NIT source properties by name
        readable var _src_local_properties: Map[Symbol, MMLocalProperty]
 
-       init(mod: MMSrcModule, n: Symbol, cla: PClassdef, a: Int)
+       init(mod: MMSrcModule, n: Symbol, cla: nullable PClassdef, a: Int)
        do
                super(mod, n, a)
-               _nodes = [cla]
+               if cla == null then
+                       _nodes = new Array[PClassdef]
+               else
+                       _nodes = [cla]
+               end
                _src_local_properties = new HashMap[Symbol, MMLocalProperty]
        end
 end
diff --git a/tests/base_no_object.nit b/tests/base_no_object.nit
new file mode 100644 (file)
index 0000000..8ed75ce
--- /dev/null
@@ -0,0 +1,13 @@
+import end
+
+class Int
+       fun output is intern
+end
+
+class A
+       fun foo do 1.output
+end
+
+var a = new A
+a.foo
+#alt1# if a is a then 2.output
diff --git a/tests/sav/base_no_object.sav b/tests/sav/base_no_object.sav
new file mode 100644 (file)
index 0000000..d00491f
--- /dev/null
@@ -0,0 +1 @@
+1
diff --git a/tests/sav/base_no_object_alt1.sav b/tests/sav/base_no_object_alt1.sav
new file mode 100644 (file)
index 0000000..1191247
--- /dev/null
@@ -0,0 +1,2 @@
+1
+2