Пример #1
0
        public MethodInterface AddMethod(string name, MethodImplementationType convention, VarType returnType, MethodParameter[] parameters, string alias = null)
        {
            if (!returnType.IsWeird && Compiler.Instance != null)
            {
                var vmType = MethodInterface.ConvertType(returnType);

                if (!ValidationUtils.IsValidMethod(name, vmType))
                {
                    throw new CompilerException("invalid method name: " + name);
                }
            }

            var method = new MethodInterface(this, convention, name, true, MethodKind.Method, returnType, parameters, alias);

            methods[name] = method;

            return(method);
        }
Пример #2
0
 public StructField(string name, VarType type)
 {
     this.name = name;
     this.type = type;
 }
Пример #3
0
 public ConstDeclaration(Scope parentScope, string name, VarType kind, string value) : base(parentScope, name)
 {
     this.Type  = kind;
     this.Value = value;
 }
Пример #4
0
 public MethodInterface AddMethod(string name, MethodImplementationType convention, VarKind returnKind, MethodParameter[] parameters, string alias = null)
 {
     return(AddMethod(name, convention, VarType.Find(returnKind), parameters, alias));
 }
Пример #5
0
 public MapDeclaration(Scope parentScope, string name, VarType keyKind, VarType valKind) : base(parentScope, name, VarType.Find(VarKind.Storage_Map), VarStorage.Global)
 {
     this.KeyKind   = keyKind;
     this.ValueKind = valKind;
 }
Пример #6
0
 public SetDeclaration(Scope parentScope, string name, VarType valKind) : base(parentScope, name, VarType.Find(VarKind.Storage_Set), VarStorage.Global)
 {
     this.ValueKind = valKind;
 }
Пример #7
0
 public VarDeclaration(Scope parentScope, string name, VarType type, VarStorage storage) : base(parentScope, name)
 {
     this.Type    = type;
     this.Storage = storage;
 }
Пример #8
0
 public MethodParameter(string name, VarType type)
 {
     Name = name;
     Type = type;
 }
Пример #9
0
 public CastExpression(Scope parentScope, VarType resultType, Expression expr) : base(parentScope)
 {
     this.expr       = expr;
     this.ResultType = resultType;
 }
Пример #10
0
        public MethodInterface AddMethod(int line, string name, bool isPublic, MethodKind kind, VarType returnType, MethodParameter[] parameters, Scope scope)
        {
            if (Methods.Count == 0)
            {
                this.LineNumber = line;
            }

            var vmType = MethodInterface.ConvertType(returnType);

            if (!ValidationUtils.IsValidMethod(name, vmType))
            {
                throw new CompilerException($"Invalid method definition: {name}:{returnType}");
            }

            var method = new MethodInterface(this.library, MethodImplementationType.Custom, name, isPublic, kind, returnType, parameters);

            this.Scope.Methods.Add(method);

            var decl = new MethodDeclaration(scope, method);

            decl.LineNumber    = line;
            this.Methods[name] = decl;

            scope.Method = decl;

            return(method);
        }
Пример #11
0
 public LiteralExpression(Scope parentScope, string value, VarType type) : base(parentScope)
 {
     this.value = value;
     this.type  = type;
 }
Пример #12
0
        public MethodInterface(LibraryDeclaration library, MethodImplementationType implementation, string name, bool isPublic, MethodKind kind, VarType returnType, MethodParameter[] parameters, string alias = null)
        {
            this.Name           = name;
            this.Library        = library;
            this.Implementation = implementation;
            this.Kind           = kind;
            this.IsPublic       = isPublic;
            this.ReturnType     = returnType;
            this.Parameters     = parameters;
            this.PreCallback    = null;
            this.PostCallback   = null;

            this.Contract = this.Library.Name;

            if (alias != null)
            {
                this.Alias = alias;
            }
            else
            {
                this.Alias = $"{char.ToUpper(this.Name[0])}{this.Name.Substring(1)}";
                if (implementation == MethodImplementationType.ExtCall)
                {
                    this.Alias = this.Library.Name + '.' + this.Alias;
                }
            }
        }
Пример #13
0
 public MethodParameter(string name, VarKind kind) : this(name, VarType.Find(kind))
 {
 }
Пример #14
0
        public static byte[] GenerateScriptFromString(VarType type, string src)
        {
            src = src.Substring(1, src.Length - 2); // remove "" delimiters

            var tokens = new List <StringToken>();

            var sb = new StringBuilder();

            bool insideTags = false;

            for (int i = 0; i < src.Length; i++)
            {
                var ch = src[i];

                switch (ch)
                {
                case '{':
                    if (insideTags)
                    {
                        throw new CompilerException("Open declaration tag mismatch");
                    }

                    if (sb.Length > 0)
                    {
                        tokens.Add(new StringToken(false, sb.ToString()));
                        sb.Clear();
                    }
                    insideTags = true;
                    break;

                case '}':
                    if (!insideTags)
                    {
                        throw new CompilerException("Close declaration tag mismatch");
                    }

                    if (sb.Length == 0)
                    {
                        throw new CompilerException("Empty declaration tag");
                    }
                    insideTags = false;
                    tokens.Add(new StringToken(true, sb.ToString()));
                    sb.Clear();
                    break;

                default:
                    sb.Append(ch);
                    break;
                }
            }

            if (sb.Length > 0)
            {
                tokens.Add(new StringToken(false, sb.ToString()));
            }

            sb.Clear();
            sb.AppendLine("POP r2"); // address
            sb.AppendLine("POP r3"); // data
            sb.AppendLine("LOAD r0 \"\"");
            foreach (var token in tokens)
            {
                if (token.dynamic)
                {
                    if (token.value == "address")
                    {
                        sb.AppendLine($"CAST r2 r1 #String");
                    }
                    else
                    if (token.value == "data")
                    {
                        if (type.Kind == VarKind.Struct)
                        {
                            throw new CompilerException($"struct fields not specified");
                        }
                        else
                        {
                            sb.AppendLine($"CAST r3 r1 #String");
                        }
                    }
                    else
                    if (token.value.StartsWith("data."))
                    {
                        throw new CompilerException($"Struct tags not implemented");
                    }
                    else
                    {
                        throw new CompilerException($"Invalid declaration tag: {token.value}");
                    }
                }
                else
                {
                    sb.AppendLine($"LOAD r1 \"{token.value}\"");
                }
                sb.AppendLine("ADD r0 r1 r0");
            }
            sb.AppendLine("PUSH r0"); // return result
            sb.AppendLine("RET");

            var asm    = sb.ToString();
            var script = AssemblerUtils.BuildScript(asm);


            return(script);
        }
Пример #15
0
 public DecimalDeclaration(Scope parentScope, string name, int decimals, VarStorage storage) : base(parentScope, name, VarType.Find(VarKind.Decimal), storage)
 {
     this.Decimals = decimals;
 }
Пример #16
0
 public StructField(string name, VarKind kind) : this(name, VarType.Find(kind))
 {
 }
Пример #17
0
 public static VMType ConvertType(VarType type)
 {
     return(ConvertType(type.Kind));
 }