Пример #1
0
        public int GetTypeID(MelonType type)
        {
            var typeKV = Types.Values.FirstOrDefault(x => x == type);

            if (typeKV == null)
            {
                throw new MelonException("Type already exists");
            }

            return(Types.First(x => x.Value == type).Key);
        }
        public Type GetTypeForOperation(OpCode op, MelonType left, MelonType right)
        {
            if (left is StringType || right is StringType)
            {
                return(typeof(StringType));
            }

            return(op switch
            {
                OpCode.ADD => (left, right) switch
                {
                    (IntegerType l, IntegerType r) => typeof(IntegerType),
                    _ => null
                },
Пример #3
0
        public MelonEngine()
        {
            GlobalEnvironment = new LexicalEnvironment(this, true);
            Strings           = new Dictionary <int, string>();
            Types             = new Dictionary <int, MelonType>();

            voidType     = AddType(new VoidType(this));
            arrayType    = AddType(new ArrayType(this));
            anyType      = AddType(new AnyType(this));
            melonType    = AddType(new MelonType(this));
            functionType = AddType(new FunctionType(this));
            integerType  = AddType(new IntegerType(this));
            floatType    = AddType(new FloatType(this));
            stringType   = AddType(new StringType(this));
            booleanType  = AddType(new BooleanType(this));

            foreach (var kv in Types)
            {
                if (kv.Value is INativeType nativeType)
                {
                    nativeType.InitProperties();
                }
            }
        }
Пример #4
0
 public FloatPrototype(MelonEngine engine, MelonType type) : base(engine, type)
 {
 }
 public BooleanPrototype(MelonEngine engine, MelonType type) : base(engine, type)
 {
 }
Пример #6
0
        public override ParseResult VisitFunctionDefinitionStatement(MelonParser.FunctionDefinitionStatementContext context)
        {
            string name = context.Name.GetText();

            LexicalEnvironment functionEnvironment = new LexicalEnvironment(parseContext.LexicalEnvironment, true);

            var functionParameters = new List <FunctionParameter>();

            if (context.Parameters != null)
            {
                for (var i = 0; i < context.Parameters.parameter().Length; i++)
                {
                    var parameter = context.Parameters.parameter(i);

                    bool isVarargs = parameter.VARARGS() != null;

                    if (isVarargs && i < context.Parameters.parameter().Length - 1)
                    {
                        throw new MelonException("Varargs parameter can only appear once and has to be the last parameter");
                    }

                    MelonType type = _engine.anyType;

                    if (parameter.Type != null)
                    {
                        var typeName = parameter.Type.name().value;
                        var typeKv   = _engine.Types.FirstOrDefault(x => x.Value.Name == typeName);

                        if (typeKv.Value == null)
                        {
                            throw new MelonException($"Could not find type '{typeName}'");
                        }

                        type = typeKv.Value;
                    }

                    var typeRef = new TypeReference(_engine, type);

                    functionEnvironment.AddVariable(parameter.Name.value, null, typeRef);
                    functionParameters.Add(new FunctionParameter(parameter.Name.value, typeRef, isVarargs));
                }
            }

            var function = new ScriptFunctionInstance(name, _engine)
            {
                ParameterTypes = functionParameters.ToArray()
            };

            var variable = parseContext.LexicalEnvironment.AddVariable(name, function, new TypeReference(_engine, _engine.functionType));

            parseContext.AddVariableReference(variable, VariableReferenceType.Local);

            if (context.ReturnType != null)
            {
                var typeName = context.ReturnType.value;
                var typeKv   = _engine.Types.FirstOrDefault(x => x.Value.Name == typeName);

                if (typeKv.Value == null)
                {
                    throw new MelonException($"Could not find type '{typeName}'");
                }

                function.ReturnType = new TypeReference(_engine, typeKv.Value);
            }

            MelonVisitor visitor = new MelonVisitor(_engine, function);
            ParseContext functionParseContext = visitor.Parse(context.Block, functionEnvironment);

            function.SetContext(functionEnvironment, functionParseContext);

            return(DefaultResult);
        }
Пример #7
0
 public IntegerPrototype(MelonEngine engine, MelonType type) : base(engine, type)
 {
 }
 public FunctionPrototype(MelonEngine engine, MelonType type) : base(engine, type)
 {
 }
Пример #9
0
 public MelonPrototype(MelonEngine engine, MelonType type) : base(engine)
 {
     Type = type;
 }
Пример #10
0
 public TypeReference(MelonEngine engine, MelonType type) : this(engine) {
     TypeId = engine.GetTypeID(type);
 }