view python/zcc.py @ 150:4ae0e02599de

Added type check start and analyze phase
author Windel Bouwman
date Fri, 01 Mar 2013 16:53:22 +0100
parents 1544e7a4aa98
children de3a68f677a5
line wrap: on
line source

#!/usr/bin/python

import sys, os, argparse
from ppci import core, frontends

parser = argparse.ArgumentParser(description='K# to bitcode compiler')
parser.add_argument('source', type=str, help='the source file to build')
args = parser.parse_args()

try:
   with open(args.source, 'r') as f:
      src = f.read()
except IOError:
   print('Failed to load {0}'.format(args.source))
   sys.exit(1)
print('stage 1: Parsing')
# Create a context and a frontend:
context = core.Context()
frontend = frontends.KsFrontend(context)
try:
   module = frontend.compilesource(src)
except core.CompilerException as e:
   print(e)
   lines = src.split(os.linesep)
   row = e.row
   col = e.col
   line = lines[row - 1]
   print('{0}:{1}'.format(row, line))
   print(' ' * col + '^')
   raise
   #sys.exit(2)

# optionally run passes here:
# TODO

print('stage 3: Code generation')
asmWriter = core.AsmWriter()
asmWriter.printModule(module)

# Generate code:
bitcodeWriter = core.BitcodeWriter()
with open(args.source + '.bc', 'wb') as f:
   bitcodeWriter.WriteModuleToFile(module, f)