Exemple #1
0
 public override void Emit(TranspilerStream stream)
 {
     stream.WriteLine("else{");
     Body.EmitDecl(stream);
     Body.EmitImpl(stream);
     stream.WriteLine("}");
 }
Exemple #2
0
        public void EmitDecl(TranspilerStream stream)
        {
            stream.Write("struct ");
            stream.Write(Compiler.Prefix);
            stream.Write(Id.Text);

            stream.WriteLine('{');
            Body?.EmitDecl(stream);
            stream.WriteLine("};\n");
        }
Exemple #3
0
        public void EmitImpl(TranspilerStream stream)
        {
            EmitReturnType(stream);
            stream.Write(' ');

            if (Block.ParentDecl is TypeDecl type)
            {
                // Member function
                type.EmitSpec(stream);
                stream.Write("::");
            }

            EmitIdAndParams(stream);
            stream.WriteLine('{');
            Body?.EmitImpl(stream);
            stream.WriteLine('}');
        }
Exemple #4
0
        public void EmitForwardDecl(TranspilerStream stream)
        {
            GenerateConstructor();

            stream.Write("struct ");
            stream.Write(Compiler.Prefix);
            stream.Write(Id.Text);
            stream.WriteLine(';');
        }
Exemple #5
0
        public override void Emit(TranspilerStream stream)
        {
            if (Condition.TypeDecl != Block.Global().FindType("Bool"))
            {
                throw new CompileError(Condition.Token,
                                       "If conditional must be a boolean expression");
            }

            stream.Write("if(!(");
            Condition.Emit(stream);
            stream.Write(".val");
            stream.WriteLine(")){");

            Body.EmitDecl(stream);
            Body.EmitImpl(stream);

            stream.WriteLine("}");
        }
Exemple #6
0
        public override void Emit(TranspilerStream stream)
        {
            // Check that we are in a fn decl.
            if (Block.ParentDecl is FnDecl fn)
            {
                if (fn.ReturnType == null)
                {
                    if (Value != null)
                    {
                        throw new CompileError(Value.Token,
                                               "Cannot return expression from 'void' procedure");
                    }
                }
                else
                {
                    if (Value == null)
                    {
                        throw new CompileError(Token,
                                               "Function must return a value");
                    }
                    else if (Value.TypeDecl != fn.Block.FindType(fn.ReturnType))
                    {
                        throw new CompileError(Value.Token,
                                               "Expression does not match function's return type");
                    }
                }
            }
            else
            {
                throw new CompileError(Token,
                                       "Cannot return from outside a function");
            }

            // Check that we are returning something that can be returned.
            if (Value is VarExpr varExpr)
            {
                if (!varExpr.VarDecl.Returnable)
                {
                    throw new CompileError(Value.Token,
                                           "Cannot return a borrowed variable");
                }
            }

            stream.Write("return ");
            Value?.Emit(stream);
            stream.WriteLine(';');
        }
Exemple #7
0
        public override void Emit(TranspilerStream stream)
        {
            // Declaration of a variable.
            var typeSpec = new SimpleTypeSpec(Value.TypeDecl.Id);
            var decl     = new VarDecl(Block, Id, typeSpec,
                                       Mutable ? VarAccess.MutableLocal : VarAccess.ImmutableLocal);

            if (!Mutable)
            {
                stream.Write("const ");
            }
            decl.TypeSpec.Emit(stream);
            stream.Write(' ');
            stream.Write(decl.CName);
            stream.Write('=');
            Value.Emit(stream);
            stream.WriteLine(';');

            Block.Vars[decl.Id.Text] = decl;
        }
Exemple #8
0
        public override void Emit(TranspilerStream stream)
        {
            var decl = Block.FindVar(
                new VarExpr(Block, Id), throws: false
                );

            if (!decl.HasValue)
            {
                throw new CompileError(Id, "Reassignment of undeclared variable");
            }

            if (!decl.Value.Mutable)
            {
                throw new CompileError(Id, "Reassignment of immutable variable");
            }

            // Reassign the value.
            stream.Write(decl.Value.CName);
            stream.Write('=');
            Value.Emit(stream);
            stream.WriteLine(';');
        }
Exemple #9
0
 public override void Emit(TranspilerStream stream)
 {
     Expr.Emit(stream);
     stream.WriteLine(";");
 }
Exemple #10
0
 public void EmitForwardDecl(TranspilerStream stream)
 {
     EmitPrototype(stream);
     stream.WriteLine(';');
 }