Beispiel #1
0
def sizeof(env: en.Environment, node: ast.NameNode) -> int:
    size = mem.MEMORY.get_type_size(node.name)

    # struct: Struct = evaluate(node, env)
    # s_name = struct.name
    # struct_size = mem.MEMORY.get_type_size(s_name)
    b = typ.int_to_bytes(size)
    return mem.MEMORY.allocate(b)
Beispiel #2
0
def malloc(length_ptr: int) -> int:
    int_len = mem.MEMORY.get_type_size("int")
    b = mem.MEMORY.get(length_ptr, int_len)
    v = typ.bytes_to_int(b)
    ptr = mem.MEMORY.malloc(v)
    ptr_b = typ.int_to_bytes(ptr)
    rtn = mem.MEMORY.allocate(ptr_b)
    return rtn
Beispiel #3
0
    def make_literal_node(self, lf, lit, make_string) -> ast.Literal:
        """
        Produce the literal node which does not contain any literal, but pointer to the stored literal bytes.

        :param self:
        :param lf: line, file
        :param lit: the literal, could be int, float, bool, or str
        :param make_string: True if make string, False if make char
        """
        # print(lit, type(lit))
        if isinstance(lit, bool):  # in python, bool is int but int is not bool
            b = typ.boolean_to_bytes(lit)
            lit_type = 2
        elif isinstance(lit, int):
            b = typ.int_to_bytes(lit)
            lit_type = 0
        elif isinstance(lit, float):
            b = typ.float_to_bytes(lit)
            lit_type = 1
        elif isinstance(lit, str):
            b = typ.string_to_bytes(lit)
            if make_string:
                lit_type = 3
            else:
                if len(b) != 1:
                    raise stl.ParseException(
                        "Only ascii characters can be char")
                lit_type = 4
        else:
            raise stl.ParseException("Unexpected literal type")

        if lit_type == 2:  # special case for boolean, since in python, bool and int has the same hash
            if lit:
                return ast.Literal(lf, 1, 2)
            else:
                return ast.Literal(lf, 0, 2)
        elif lit in self.literals:
            pos = self.literals[lit]
            node = ast.Literal(lf, pos, lit_type)
            return node
        else:
            ptr = len(self.literal_bytes)
            self.literals[lit] = ptr
            self.literal_bytes.extend(b)
            node = ast.Literal(lf, ptr, lit_type)
            if lit_type == 3:
                self.string_lengths[ptr] = len(b)
            return node
Beispiel #4
0
def eval_pack(vp: int, v_tal: en.Type, env: en.Environment):
    b = typ.int_to_bytes(vp)
    return mem.MEMORY.allocate(b)
Beispiel #5
0
def clock() -> int:
    t_s = time.time()
    t_ms = int(t_s * 1000)
    b = typ.int_to_bytes(t_ms)
    return mem.MEMORY.allocate(b)
Beispiel #6
0
def heap_ava() -> int:
    size = len(mem.MEMORY.available2)
    b = typ.int_to_bytes(size)
    return mem.MEMORY.allocate(b)
Beispiel #7
0
 def write_int(self, i: int, index=None):
     b = typ.int_to_bytes(i)
     if index is None:
         self.res.extend(b)
     else:
         self.res[index: index + len(b)] = b