From 838d4f0ebe04cad50d26fbc6a6ff54beb6f883a5 Mon Sep 17 00:00:00 2001 From: nkorslund Date: Tue, 30 Dec 2008 02:53:32 +0000 Subject: [PATCH] More scripting updates git-svn-id: https://openmw.svn.sourceforge.net/svnroot/openmw/trunk@78 ea6a568a-9f4f-0410-981a-c910a81bb256 --- monster/compiler/expression.d | 8 +++++++- monster/compiler/functions.d | 4 ++-- monster/compiler/types.d | 25 ++++++++++++++++--------- monster/compiler/variables.d | 4 ++-- monster/vm/mclass.d | 16 ++++++++++------ mscripts/{gamesettings.mn => gmst.mn} | 4 ++-- mscripts/test.mn | 3 ++- scene/gamesettings.d | 16 ++++++++-------- 8 files changed, 49 insertions(+), 31 deletions(-) rename mscripts/{gamesettings.mn => gmst.mn} (99%) diff --git a/monster/compiler/expression.d b/monster/compiler/expression.d index 83d333655..6a1e56dbe 100644 --- a/monster/compiler/expression.d +++ b/monster/compiler/expression.d @@ -518,7 +518,13 @@ class NewExpression : Expression { // We need to find the index associated with this class, and // pass it to the assembler. - clsInd = (cast(ObjectType)type).getClass().getIndex(); + auto mc = (cast(ObjectType)type).getClass(); + assert(mc !is null); + clsInd = mc.getIndex(); + + // Don't create instances of modules! + if(mc.isModule) + fail("Cannot create instances of modules", loc); } else if(type.isArray) { diff --git a/monster/compiler/functions.d b/monster/compiler/functions.d index c7f642e28..c1ad6e01a 100644 --- a/monster/compiler/functions.d +++ b/monster/compiler/functions.d @@ -635,9 +635,9 @@ class FuncDeclaration : Statement { // Validate all types (make sure there are no dangling forward // references) - fn.type.validate(); + fn.type.validate(fn.name.loc); foreach(p; fn.params) - p.type.validate(); + p.type.validate(fn.name.loc); if(code !is null) code.resolve(sc); diff --git a/monster/compiler/types.d b/monster/compiler/types.d index ab4e2bcc4..e4a8ddfbf 100644 --- a/monster/compiler/types.d +++ b/monster/compiler/types.d @@ -231,7 +231,7 @@ abstract class Type : Block // Validate that this type actually exists. This is used to make // sure that all forward references are resolved. - void validate() {} + void validate(Floc loc) {} // Check if this type is equivalent to the given D type final bool isDType(TypeInfo ti) @@ -777,7 +777,7 @@ class ObjectType : Type clsIndex = mc.gIndex; } - MonsterClass getClass() + MonsterClass getClass(Floc loc = Floc.init) { assert(clsIndex != 0); @@ -789,8 +789,15 @@ class ObjectType : Type } override: - // getClass does all the error checking we need - void validate() { getClass(); } + void validate(Floc loc) + { + // getClass does most of the error checking we need + auto mc = getClass(loc); + + // check that we're not using a module as a type + if(mc.isModule) + fail("Cannot use module " ~ name ~ " as a type", loc); + } int getSize() { return 1; } bool isObject() { return true; } @@ -847,8 +854,8 @@ class ObjectType : Type // This is called when the type is defined, and it can forward // reference classes that do not exist yet. The classes must exist - // by the time getClass() is called though, usually when class body - // (not just the header) is being resolved. + // by the time getClass() is called though, usually when the class + // body (not just the header) is being resolved. void resolve(Scope sc) { clsIndex = global.getForwardIndex(name); @@ -884,7 +891,7 @@ class ArrayType : Type } override: - void validate() { assert(base !is null); base.validate(); } + void validate(Floc loc) { assert(base !is null); base.validate(loc); } int arrays() { return base.arrays() + 1; } int getSize() { return 1; } int[] defaultInit() { return makeData!(int)(0); } @@ -975,10 +982,10 @@ class StructType : Type override: // Calls validate for all member types - void validate() + void validate(Floc loc) { foreach(v; vars) - v.type.validate(); + v.type.validate(loc); } // Resolves member diff --git a/monster/compiler/variables.d b/monster/compiler/variables.d index 23c4a463d..1def42cf7 100644 --- a/monster/compiler/variables.d +++ b/monster/compiler/variables.d @@ -375,7 +375,7 @@ class VarDeclaration : Block void compile() { // Validate the type - var.type.validate(); + var.type.validate(loc); setLine(); @@ -813,7 +813,7 @@ class VarDeclStatement : Statement void validate() { assert(vars.length >= 1); - vars[0].var.type.validate(); + vars[0].var.type.validate(loc); } // Insert local variable(s) on the stack. diff --git a/monster/vm/mclass.d b/monster/vm/mclass.d index dc002df38..d5e8f8c63 100644 --- a/monster/vm/mclass.d +++ b/monster/vm/mclass.d @@ -478,7 +478,8 @@ final class MonsterClass // Get the singleton object MonsterObject* getSing() { - assert(isSingleton()); + if(!isSingleton) + fail("Class is not a singleton: " ~ name.str); requireCompile(); assert(singObj !is null); return singObj; @@ -492,6 +493,9 @@ final class MonsterClass if(isAbstract) fail("Cannot create objects from abstract class " ~ name.str); + if(isModule && singObj !is null) + fail("Cannot create instances of module " ~ name.str); + // Create the thread CodeThread *trd = threads.getNew(); @@ -588,6 +592,9 @@ final class MonsterClass { assert(obj.cls is this); + if(isModule) + fail("Cannot delete instances of module " ~ name.str); + // Get the head object obj = obj.thread.topObj; @@ -1049,11 +1056,8 @@ final class MonsterClass if(!isNext(tokens, TT.Identifier, name)) fail("Class statement expected identifier", tokens); - if(isModule) - { - assert(isSingleton); - fail("Modules are not implement yet.", name.loc); - } + // Module implies singleton + assert(isSingleton || !isModule); if(isSingleton && isAbstract) fail("Modules and singletons cannot be abstract", name.loc); diff --git a/mscripts/gamesettings.mn b/mscripts/gmst.mn similarity index 99% rename from mscripts/gamesettings.mn rename to mscripts/gmst.mn index f8aa788e1..0d2e4b2de 100644 --- a/mscripts/gamesettings.mn +++ b/mscripts/gmst.mn @@ -24,7 +24,7 @@ // Contains all the game settings (GMST) variables of Morrowind, // Tribunal and Bloodmoon. Based on "Morrowind Scripting for Dummies" // (v9). -singleton GameSettings : Object; +singleton GMST : Object; // Most of the comments are copied from MSfD. A bit of cleanup is // still needed. @@ -2090,7 +2090,7 @@ char[] sMagicCreature05ID; test() { // Print out some random variables. If you want these not to get - // lost in a see of output from OGRE, run openmw with the -n switch + // lost in a sea of output from OGRE, run openmw with the -n switch // to disable rendering. print(); print("Some random GMST variables:"); diff --git a/mscripts/test.mn b/mscripts/test.mn index 929645da0..e3d0d129f 100644 --- a/mscripts/test.mn +++ b/mscripts/test.mn @@ -10,11 +10,12 @@ state printMessage { // This state code will run as long as the object is in this state. begin: - sleep(10); + sleep(6); loop: print("Howdy from the world of Monster scripts!"); print("This script is located in mscripts/test.mn. Check it out!"); + print("GMST.sThief: ", GMST.sThief); sleep(60); goto loop; } diff --git a/scene/gamesettings.d b/scene/gamesettings.d index 636585716..f4fbe851c 100644 --- a/scene/gamesettings.d +++ b/scene/gamesettings.d @@ -10,10 +10,10 @@ MonsterObject *gmstObj; void loadGameSettings() { - // Load the GameSettings Monster class, and create an instance. - MonsterClass mc = new MonsterClass("GameSettings"); - MonsterObject *mo = mc.createObject; - gmstObj = mo; + // Load the GameSettings Monster class, and get the singleton + // instance + MonsterClass mc = MonsterClass.find("GMST"); + gmstObj = mc.getSing(); foreach(a, b; gameSettings.names) { @@ -38,13 +38,13 @@ void loadGameSettings() continue; } - if(b.type == VarType.Int) mo.setInt(name, b.i); - else if(b.type == VarType.Float) mo.setFloat(name, b.f); + if(b.type == VarType.Int) gmstObj.setInt(name, b.i); + else if(b.type == VarType.Float) gmstObj.setFloat(name, b.f); // TODO: At some point we will probably translate strings into // UTF32 at load time, so string8 will not be needed here. - else if(b.type == VarType.String) mo.setString8(name, b.str); + else if(b.type == VarType.String) gmstObj.setString8(name, b.str); } // Call the test function - mo.call("test"); + gmstObj.call("test"); }