Esempio n. 1
0
    def compileToC(self, codegen):
        self.typ.toCType()

        #if self.typ.package == "_global" and type(self.typ) in [Types.Struct, Types.Enum]:
        #    codegen.append("NULL")
        #    return

        package = self.typ.package if self.typ.package != "" else "_global"
        fullName = SimplifyAst.toUniqueID(package, self.typ.normalName, self.typ.remainingGen)

        if not type(self.typ) in [Types.Interface, Types.Pointer, Types.Alias, Types.Null]:
            if type(self.typ) is Types.T:
                typeof_none = Typeof(self, Types.Null())
                Tree.castFrom(typeof_none.type, Parser.IType, typeof_none, "", codegen)
            else:
                codegen.append(f"{fullName}_get_type(NULL," + codegen.getContext() + ")")
        else:
            if type(self.typ) is Types.Pointer:
                codegen.append("_global_boxPointerType(_global_PointerTypeInit(")
                tmp = Typeof(self, self.typ.pType)
                Cast.castFrom(tmp.type, Parser.IType, tmp, "", codegen)
                codegen.append(",0)," + codegen.getContext() + ")")
            elif type(self.typ) is Types.Null:
                codegen.append(f"&None_Type")
            else:
                codegen.append(f"&{fullName}_Type")
Esempio n. 2
0
    def compileToC(self, codegen):
        codegen.inFunction()

        iName = f"{self.newName}_Type;"
        codegen.append(f"{Parser.PointerType.toCType()} {iName};")
        codegen.outFunction()

        codegen.append(f"{iName}.pType = ")
        tmp = Typeof(self, self.pType)
        Cast.castFrom(tmp.type, Parser.IType, tmp, "", codegen)
        codegen.append(";")
Esempio n. 3
0
    def compileToC(self, codegen):
        def as_string(s):
            return f'_global_StringInit({len(s)}, "{s}")'

        iName = f"{self.package}_{self.structName}_Type"

        if type(Parser.AliasType) is Parser.TmpType: return

        codegen.inFunction()
        codegen.append(f"{Parser.AliasType.toCType()} {iName};")
        codegen.outFunction()

        codegen.append(f"{iName}.name = {as_string(self.structName)};" )
        codegen.append(f"{iName}.package = {as_string(self.package)};")
        codegen.append(f"{iName}.real_type = ")

        tmp = Typeof(self, self.typ.typ)
        Cast.castFrom(tmp.type, Parser.IType, tmp, "", codegen)
        codegen.append(";")
Esempio n. 4
0
    def compileToC(self, codegen):
        if self.kind == "|>":
            self.nodes[1].compileToC(codegen)
            codegen.append("(")
            self.nodes[0].compileToC(codegen)
            codegen.append(")")
            return
        elif self.kind == "..":
            codegen.append("_global_RangeInit(")
            it = 0
            for i in self.nodes:
                it += 1
                i.compileToC(codegen)
                if it != len(self.nodes):
                    codegen.append(",")
            codegen.append(")")
            return
        elif self.kind == "as":
            return Cast.castFrom(self.nodes[0].type, self.type, self.nodes[0],
                                 {}, codegen)
        elif self.kind == ">>":
            a = self.nodes[0].type
            b = self.nodes[1].type

            codegen.append("function(")
            names = ",".join(
                [codegen.getName() for i in self.nodes[0].type.args])
            codegen.append(names)
            codegen.append("){return ")
            self.nodes[1].compileToJS(codegen)
            codegen.append("(")
            self.nodes[0].compileToJS(codegen)
            codegen.append("(" + names + "))}")
            return

        if self.overload:
            if self.interface:
                if len(self.nodes) == 0:
                    codegen.append(self.name)
                    return
                self.nodes[0].compileToC(codegen)
                codegen.append("." + self.name + "(")
            elif self.overload:
                codegen.append(self.package + "_" + self.name)
                codegen.append("(")
                self.nodes[0].compileToC(codegen)

            if len(self.nodes) > 1:
                codegen.append(",") if not self.interface else 0
                self.nodes[1].compileToC(codegen)
            codegen.append(")")
            return

        if self.kind == "concat":
            codegen.append("_global_String_append_tmp")
        if self.kind == "+" and type(self.type) is Types.String:
            codegen.append("_global_String_op_addByValue")

        if self.kind == "^":
            codegen.append("powf(")

        if self.kind in ["+", "-", "*", "/", "<", ">", "<=", ">=", "&"]:
            op = self.kind

        else:
            translate = {
                "==": "==",
                "!=": "!=",
                "not": "!",
                "and": "&&",
                "or": "||",
                "%": "%",
                "concat": ")+(",
                "^": ",",
                "&mut": "&"
            }

            op = translate[self.kind]

        if self.unary:
            codegen.append(op)
            codegen.append("(")

        self.nodes[0].compileToC(codegen)

        if not self.unary:
            if self.kind == "concat" or (self.kind == "+"
                                         and type(self.type) is Types.String):
                codegen.append(",")
            else:
                codegen.append(op)

            self.nodes[1].compileToC(codegen)
            if self.kind == "concat":
                pass
                #@cleanup implement toString later
                #codegen.append(").toString()")
            if self.kind == "^":
                codegen.append(")")

        if self.unary:
            codegen.append(")")
Esempio n. 5
0
 def validate(self, parser):
     if self.kind == "as":
         Cast.checkCast(self.nodes[0].type, self.type, self.nodes[0],
                        parser)
Esempio n. 6
0
    def compileToC(self, codegen):
        codegen.inFunction()

        elemT = self.arrType.remainingGen["StaticArray.T"]
        numElements = self.arrType.remainingGen["StaticArray.S"]

        codegen.append(f"struct {self.structName} {{\n")
        codegen.append(f"{elemT.toCType()} data[{numElements}];\n")
        codegen.append("};\n")

        #fill Array, -syntax [8: 3] == [3,3,3,3,3,3,3,3,3,3]
        codegen.append(
            f"struct {self.structName} {self.structName}Fill_array(")
        codegen.append(elemT.toCType() + " with")
        codegen.append("){\n")
        codegen.append(f"struct {self.structName} tmp;\n")
        codegen.append(
            f"for (unsigned int i = 0; i < {numElements}; i++) {{\n")
        codegen.append("tmp.data[i] = with;\n")
        codegen.append("}; return tmp; }\n")

        codegen.append(f"struct {self.structName} {self.structName}Init(")

        names = []
        cElemT = elemT.toCType()
        for i in range(numElements):
            name = codegen.getName()
            names.append(name)

            codegen.append(cElemT + " " + name)
            if i < numElements - 1:
                codegen.append(",")
        codegen.append("){\n")
        codegen.append(f"struct {self.structName} tmp;\n")
        for (it, name) in enumerate(names):
            codegen.append(f"tmp.data[{it}] = {name};\n")
        codegen.append("return tmp; }\n")

        # Introspection
        def as_string(s):
            return f'_global_StringInit({len(s)}, "{s}")'

        structName = self.structName

        nameOfI = f"{structName}Type"

        codegen.append("struct _global_ArrayType " + nameOfI + ";")

        codegen.append(
            f"struct _global_ArrayType* {self.structName}_get_type(struct {structName}* self, struct _global_Context* c)"
            + "{")
        codegen.append(f"return &{self.structName}Type;")
        codegen.append("}\n")

        codegen.append(
            f"struct _global_ArrayType* {self.structName}_get_typeByValue(struct {structName} self, struct _global_Context* c)"
            + "{")
        codegen.append(f"return &{self.structName}Type;")
        codegen.append("}\n")

        codegen.append(f"{Parser.ArrayType.toCType()} {self.structName}Type;")

        codegen.outFunction()

        codegen.append(
            f"{nameOfI}.size = malloc(sizeof(struct _global_ArraySize));")
        codegen.append(f"{nameOfI}.size->tag = 0;")
        codegen.append(f"{nameOfI}.size->cases.Static.field0 = {numElements};")
        codegen.append(f"{nameOfI}.array_type = ")

        t = Tree.Typeof(self, elemT)
        Cast.castFrom(t.type, Parser.IType, t, "", codegen)

        codegen.append(";")
Esempio n. 7
0
    def compileToC(self, codegen):
        #if self.nodes[0].type.name == "uint" and self.type.name == "u64":
        #    print("what")

        #castFrom(originalType, newType, node, realName, codegen):
        Cast.castFrom(self.nodes[0].type, self.type, self.nodes[0], "", codegen)