def test_types(self): self.assertEqualToSource(code.VoidType(), "void") self.assertEqualToSource(code.IntegerType(), "int") self.assertEqualToSource(code.LongType(), "long") self.assertEqualToSource(code.FloatType(), "float") self.assertEqualToSource(code.ByteType(), "char") self.assertEqualToSource(code.BooleanType(), "int")
def test_structured_type_tagging(self): section = Section("test") struct = code.StructuredType("something") struct.tag("struct_something") section.append(struct) section.select("struct_something").append( code.Property("prop1", code.FloatType())) self.assertEqualToSource( struct, "typedef struct something_t {\nfloat prop1;\n} something_t;")
def visit_MethodCall(self, call): """ Methodcalls to the nodes domain are rewritten to function-calls. """ if isinstance(call.obj, code.Object) and call.obj.name == "nodes": # FIXME: this is a bit too local, would nicer at C-level, based on the # definition of the parameters of these functions. not for now ;-) # these function call take vararg bytes. we need to convert each non-byte # argument to a ListLiteral of corresponding bytes. # create function-call # strategy: # AtomLiteral will be expanded later, so we skip them here # ObjectProperty check/convert property type # SimpleVariable # FunctionCall extract from arguments, assign to variable, convert assert isinstance(call.arguments[0], code.ListLiteral) # TODO: mark this somehow and move logic to emitter # the UnionType solution is to C-specific - no more time to do it # right :-( # rebuild the arguments args = code.ListLiteral() temp = 0 for index, arg in enumerate(call.arguments[0]): # TODO: generalize a bit more - this only support our minimal needs if isinstance(arg, code.FunctionCall): code.Assign(code.VariableDecl("temp" + str(temp), arg.type), arg) \ .insert_before(call) if isinstance(arg.type, code.AmountType) and \ isinstance(arg.type.type, code.ByteType): for i in range(arg.type.size): args.append( code.ListVariable("temp" + str(temp), i)) temp += 1 elif isinstance(arg, code.SimpleVariable): if str(arg.info) == "TimestampType": code.VariableDecl( "temp" + str(temp), code.UnionType("temp" + str(temp)) \ .contains( code.Property("value", code.NamedType("timestamp")), code.Property( "b", code.AmountType(code.ByteType(), 4) # platf? ) ) ).insert_before(call) code.Assign(code.StructProperty("temp" + str(temp), "value"), arg) \ .insert_before(call) for i in range(4): args.append( code.ListVariable( code.StructProperty( "temp" + str(temp), "b"), i)) temp += 1 elif str(arg.info) == "ObjectType(nodes)": # a node is identified in a distributed manner by its nw address # TODO: maybe not always correct, but split it out as 2 bytes args.append( code.ShiftLeft( code.ObjectProperty(arg.name, "address"), 8), code.ObjectProperty(arg.name, "address")) elif isinstance(arg, code.ObjectProperty): if isinstance(arg.type, code.ByteType): args.append(arg) elif isinstance(arg.type, code.FloatType): code.VariableDecl( "temp" + str(temp), code.UnionType("temp" + str(temp)) \ .contains( code.Property("value", code.FloatType() ), code.Property( "b", code.AmountType(code.ByteType(), 4) ) ) ).insert_before(call) code.Assign(code.StructProperty("temp" + str(temp), "value"), arg) \ .insert_before(call) for i in range(4): args.append( code.ListVariable( code.StructProperty( "temp" + str(temp), "b"), i)) temp += 1 else: args.append(arg) # replace by functioncall return code.FunctionCall("nodes_" + call.method.name, [args])
def after_visit_FloatType(self, type): self.code.append(code.FloatType())
def test_function_with_type(self): tree = code.Function("name", type=code.FloatType()) self.assertEqualToSource(tree, "float name(void) {\n\n}")
def test_append_property_to_structured_type(self): struct = code.StructuredType("something") struct.append(code.Property("prop1", code.FloatType())) self.assertEqualToSource( struct, "typedef struct something_t {\nfloat prop1;\n} something_t;")