public Handle Create(TItem value)
        {
            var raw    = _serialize(value);
            var handle = _heap.Allocate(raw.Length);

            _heap.Write(handle, raw);
            return(handle);
        }
        private static Handle AllocateAndWrite(IHeap <byte> heap, string text)
        {
            var bytes  = Encoding.UTF8.GetBytes(text);
            var handle = heap.Allocate(bytes.Length);

            heap.Write(handle, bytes);
            return(handle);
        }
Ejemplo n.º 3
0
        private IWasmicSyntaxTree GetLocalVariableDeclaration()
        {
            _lexer.Advance(); // eat var
            _lexer.AssertNext(TokenType.Identifier);
            var name = _lexer.Next.Value;

            if (_localVariables.Contains(name))
            {
                throw new WasmicCompilerException($"local variable {name} is already declared");
            }

            _lexer.Advance(); // eat name
            string type = null;

            if (_lexer.Next.TokenType == TokenType.Colon)
            {
                _lexer.Advance(); // eat :
                type = _lexer.Next.Value;
                _lexer.Advance(); // eat type;
            }

            IWasmicSyntaxTreeExpression tree = null;

            switch (_lexer.Next.TokenType)
            {
            case TokenType.Equal:
                _lexer.Advance();     // eat =

                if (_lexer.Next.TokenType == TokenType.New)
                {
                    _lexer.Advance();     // eat new
                    _lexer.AssertNext(TokenType.Identifier);
                    var arrayType = _lexer.Next.Value;
                    _lexer.Advance();     // eat type
                    _lexer.AssertNext(TokenType.L_Bracket);
                    _lexer.Advance();     // eat [

                    _lexer.AssertNext(TokenType.Int32);
                    var size = int.Parse(_lexer.Next.Value);
                    _lexer.Advance();     // eat size

                    _lexer.AssertNext(TokenType.R_Bracket);
                    _lexer.Advance();     // eat ]

                    if (arrayType != "i32")
                    {
                        throw new NotImplementedException("arrays other than i32");
                    }
                    var heapOffset = _heap.Allocate(size * 4);

                    type = "i32[]";
                    tree = GetSetLocalVariable(name, new Literal("i32", heapOffset.ToString()));
                }
                else
                {
                    tree = GetSetLocalVariable(name, null, true);
                    if (type != null && tree.Type != type)
                    {
                        throw new WasmicCompilerException($"types do not match: {type} / {tree.Type}");
                    }
                    type = tree.Type;
                }

                break;

            case TokenType.SemiColon:
                if (type == null)
                {
                    throw new WasmicCompilerException($"variable {name} must have a type declaration");
                }
                _lexer.Advance();     // eat ;
                break;
            }

            if (type == null)
            {
                throw new NotImplementedException(nameof(type));
            }
            _localVariables.AddLocalVariable(name, type);
            return(tree);
        }
Ejemplo n.º 4
0
 internal protected long Allocate(long aBytes) =>
 mrStreamSpaceAllocator.Allocate(aBytes);