diff python/c3/typecheck.py @ 222:c3f1ce8b638f

Fixup of parser
author Windel Bouwman
date Tue, 09 Jul 2013 17:36:31 +0200
parents 848c4b15fd0b
children 1c7364bd74c7
line wrap: on
line diff
--- a/python/c3/typecheck.py	Mon Jul 08 22:21:44 2013 +0200
+++ b/python/c3/typecheck.py	Tue Jul 09 17:36:31 2013 +0200
@@ -11,6 +11,13 @@
         return equalTypes(a.ptype, b.ptype)
    return False
 
+def canCast(fromT, toT):
+    if isinstance(fromT, PointerType) and isinstance(toT, PointerType):
+        return True
+    elif fromT is intType and isinstance(toT, PointerType):
+        return True
+    return False
+
 class TypeChecker:
     def __init__(self, diag):
         self.diag = diag
@@ -118,10 +125,15 @@
          # check initial value type:
          # TODO
          pass
+      elif type(sym) is TypeCast:
+            if canCast(sym.a.typ, sym.to_type):
+                sym.typ = sym.to_type
+            else:
+                self.error('Cannot cast {} to {}'.format(sym.a.typ, sym.to_type))
       elif type(sym) is Constant:
          if not equalTypes(sym.typ, sym.value.typ):
             self.error('Cannot assign {0} to {1}'.format(sym.value.typ, sym.typ), sym.loc)
-      elif type(sym) in [EmptyStatement, CompoundStatement, Package, Function, FunctionType]:
+      elif type(sym) in [EmptyStatement, CompoundStatement, Package, Function, FunctionType, ExpressionStatement]:
          pass
       else:
             raise Exception('Unknown type check {0}'.format(sym))