def test_mismatch_type_mod(self) -> None: with self.assertRaises(pt.TypeCheckError): pt.Program("module").add_func( "foo", (), pt.Int32_t, pt.Block([pt.Return(pt.Mod(pt.Int8(2), pt.Int32(2)))]), ) with self.assertRaises(pt.TypeCheckError): pt.Program("module").add_func( "foo", (), pt.Int32_t, pt.Block([pt.Return(pt.Mod(pt.Int32(2), pt.Int8(2)))]), ) with self.assertRaises(pt.TypeCheckError): pt.Program("module").add_func( "foo", (), pt.Int64_t, pt.Block([pt.Return(pt.Mod(pt.Int16(2), pt.Int64(2)))]), ) with self.assertRaises(pt.TypeCheckError): pt.Program("module").add_func( "foo", (), pt.Int64_t, pt.Block([pt.Return(pt.Mod(pt.Int64(2), pt.Int16(2)))]), )
def test_assign_undeclared_variable(self) -> None: with self.assertRaises(pt.TypeCheckError): pt.Program("module").add_func( "foo", (), pt.Int32_t, pt.Block([ pt.Assign(pt.Var(x), pt.Int32(500)), pt.Return(pt.Int32(2)), ]), )
def test_redeclared_variable(self) -> None: with self.assertRaises(pt.TypeCheckError): pt.Program("module").add_func( "foo", (), pt.Int32_t, pt.Block([ pt.DefineVar(x, pt.Int32(2)), pt.DefineVar(x, pt.Int32(3)), pt.Return(pt.Int32(2)), ]), )
def test_declare_wrong_type(self) -> None: with self.assertRaises(pt.TypeCheckError): pt.Program("module").add_func( "foo", (), pt.Int32_t, pt.Block([ pt.DefineVar(z, pt.Int32(2)), pt.Return(pt.Int32(2)), ]), ) with self.assertRaises(pt.TypeCheckError): pt.Program("module").add_func( "foo", (), pt.Int32_t, pt.Block([ pt.DefineVar(x, pt.Int8(2)), pt.Return(pt.Int32(2)), ]), )
a32 = pt.Symbol(pt.Int32_t, "a32") b32 = pt.Symbol(pt.Int32_t, "b32") c32 = pt.Symbol(pt.Int32_t, "c32") d32 = pt.Symbol(pt.Int32_t, "d32") e32 = pt.Symbol(pt.Int32_t, "e32") f32 = pt.Symbol(pt.Int32_t, "f32") g32 = pt.Symbol(pt.Int32_t, "g32") h32 = pt.Symbol(pt.Int32_t, "h32") i32 = pt.Symbol(pt.Int32_t, "i32") program.add_func( "add_i32", (), pt.Int32_t, pt.Block([ pt.DefineVar(a32, pt.Add(pt.Int32(-11), pt.Int32(-4))), pt.DefineVar(b32, pt.Add(pt.Int32(-11), pt.Int32(0))), pt.DefineVar(c32, pt.Add(pt.Int32(-11), pt.Int32(7))), pt.DefineVar(d32, pt.Add(pt.Int32(0), pt.Int32(-5))), pt.DefineVar(e32, pt.Add(pt.Int32(0), pt.Int32(0))), pt.DefineVar(f32, pt.Add(pt.Int32(0), pt.Int32(3))), pt.DefineVar(g32, pt.Add(pt.Int32(7), pt.Int32(-8))), pt.DefineVar(h32, pt.Add(pt.Int32(7), pt.Int32(0))), pt.DefineVar(i32, pt.Add(pt.Int32(7), pt.Int32(8))), pt.Return( pt.Add( pt.Var(a32), pt.Add( pt.Var(b32), pt.Add( pt.Var(c32),
import petra as pt program = pt.Program("module") n = pt.Symbol(pt.Int32_t, "n") program.add_func( "collatz", (n, ), pt.Int32_t, pt.Block([ pt.If( pt.Eq(pt.Var(n), pt.Int32(1)), pt.Block([pt.Return(pt.Int32(0))]), pt.Block([]), ), pt.If( pt.Eq(pt.Mod(pt.Var(n), pt.Int32(2)), pt.Int32(0)), pt.Block([pt.Assign(pt.Var(n), pt.Div(pt.Var(n), pt.Int32(2)))]), pt.Block([ pt.Assign( pt.Var(n), pt.Add(pt.Mul(pt.Int32(3), pt.Var(n)), pt.Int32(1)), ), ]), ), pt.Return(pt.Add(pt.Int32(1), pt.Call("collatz", [pt.Var(n)]))), ]), ) program.save_object("collatz.py.o")
def test_above_bounds_i32(self) -> None: with self.assertRaises(pt.ValidateError): pt.Int32(2**31)
def test_below_bounds_i32(self) -> None: with self.assertRaises(pt.ValidateError): pt.Int32(-(2**31) - 1)
# Int16_t functions. program.add_func("return_m2_i16", (), pt.Int16_t, pt.Block([pt.Return(pt.Int16(-2))])) program.add_func("return_0_i16", (), pt.Int16_t, pt.Block([pt.Return(pt.Int16(0))])) program.add_func("return_2_i16", (), pt.Int16_t, pt.Block([pt.Return(pt.Int16(2))])) # Int32_t functions. program.add_func("return_m2_i32", (), pt.Int32_t, pt.Block([pt.Return(pt.Int32(-2))])) program.add_func("return_0_i32", (), pt.Int32_t, pt.Block([pt.Return(pt.Int32(0))])) program.add_func("return_2_i32", (), pt.Int32_t, pt.Block([pt.Return(pt.Int32(2))])) # Int64_t functions. program.add_func("return_m2_i64", (), pt.Int64_t, pt.Block([pt.Return(pt.Int64(-2))])) program.add_func("return_0_i64", (), pt.Int64_t, pt.Block([pt.Return(pt.Int64(0))]))
from ctypes import CFUNCTYPE, c_int32 program = pt.Program("module") My_Array = pt.ArrayType(pt.Int32_t, 3) array_var = pt.Symbol(My_Array, "array_var") program.add_func( "array_set_get_values", (), pt.Int32_t, pt.Block( [ pt.DefineVar(array_var), pt.Assign( pt.Var(array_var), pt.SetElement(pt.Var(array_var), pt.Int32(1), 0) ), pt.Assign( pt.Var(array_var), pt.SetElement(pt.Var(array_var), pt.Int32(2), 1) ), pt.Assign( pt.Var(array_var), pt.SetElement(pt.Var(array_var), pt.Int32(3), 2) ), pt.Return( pt.Add( pt.GetElement(pt.Var(array_var), 0), pt.Add( pt.GetElement(pt.Var(array_var), 1), pt.GetElement(pt.Var(array_var), 2), ), )
import petra as pt import unittest from ctypes import CFUNCTYPE, c_int32 program = pt.Program("module") x = pt.Symbol(pt.Int32_t, "x") y = pt.Symbol(pt.Int32_t, "y") z = pt.Symbol(pt.Int8_t, "z") program.add_func( "return_temp", (), pt.Int32_t, pt.Block([pt.DefineVar(x, pt.Int32(2)), pt.Return(pt.Var(x))]), ) program.add_func( "return_shuffle_temp", (), pt.Int32_t, pt.Block([ pt.DefineVar(x, pt.Int32(2)), pt.DefineVar(y, pt.Var(x)), pt.Return(pt.Var(y)), ]), ) program.add_func(
from typing import cast, Callable import petra as pt import unittest from ctypes import CFUNCTYPE, c_int32 program = pt.Program("module") x, y = pt.Symbol(pt.Int32_t, "x"), pt.Symbol(pt.Int32_t, "y") program.add_func("return_2", (), pt.Int32_t, pt.Block([pt.Return(pt.Int32(2))])) program.add_func("call_return_2", (), pt.Int32_t, pt.Block([pt.Return(pt.Call("return_2", []))])) program.add_func( "call_return_2_discard", (), pt.Int32_t, pt.Block([pt.Call("return_2", []), pt.Return(pt.Int32(3))]), ) program.add_func( "iden", (x, ), pt.Int32_t, pt.Block([pt.Return(pt.Var(x))]), )
# void* memset ( void* ptr, int value, size_t num ); program.add_func_decl("memset", (Pointer_Int64_t, pt.Int32_t, pt.Int64_t), Pointer_Int64_t) ptr = pt.Symbol(Pointer_Int64_t, "ptr") val = pt.Symbol(pt.Int64_t, "val") v32 = pt.Symbol(pt.Int32_t, "val") program.add_func( "malloc_free", (), pt.Int32_t, pt.Block([ pt.DefineVar(ptr, pt.Call("malloc", [pt.Int64(8)])), pt.Call("free", [pt.Var(ptr)]), pt.Return(pt.Int32(1)), ]), ) program.add_func( "malloc_memset", (), pt.Int64_t, pt.Block([ pt.DefineVar(ptr, pt.Call("malloc", [pt.Int64(8)])), pt.Assign( pt.Var(ptr), pt.Call("memset", [pt.Var(ptr), pt.Int32(0x3A), pt.Int64(8)]), ),
from typing import cast, Callable import petra as pt import unittest from ctypes import CFUNCTYPE, c_int32, c_bool program = pt.Program("module") program.add_func( "if_else_true", (), pt.Int32_t, pt.Block([ pt.If( pt.Lt(pt.Int32(0), pt.Int32(1)), pt.Block([ pt.Return(pt.Int32(1)), ]), pt.Block([pt.Return(pt.Int32(2))]), ), pt.Return(pt.Int32(0)), ]), ) program.add_func( "if_else_false", (), pt.Int32_t, pt.Block([ pt.If(