Wrap up the new file/dir handling.

An .outfile directive lets sources suggest default filenames.

Also, .include, .require, .incbin, and .charmapbin are relative
to their _source file_ as opposed the _directory you called Ophis
from_, like it really should have always been.
This commit is contained in:
Michael Martin 2012-06-03 19:45:55 -07:00
parent 17f68399ef
commit cf0df92fb1
8 changed files with 48 additions and 20 deletions

View File

@ -1,6 +1,8 @@
.include "../c64_0.oph"
.include "../c64kernal.oph"
.outfile "hello.prg"
ldy #$00
* lda text, y
beq +

View File

@ -1,5 +1,7 @@
; iNES header
.byte $4e,$45,$53,$1a,$01,$01,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00
.outfile "hello.nes"
.include "hello_prg.oph"
.include "hello_chr.oph"

View File

@ -1,3 +1,4 @@
.outfile "hello.unf"
.byte "UNIF"
.dword 7
.advance $20

View File

@ -1,4 +1,5 @@
.require "../stella.oph"
.outfile "hi_stella.bin"
.data
.org $0080

View File

@ -6,6 +6,7 @@
# You may use, modify, and distribute this file under the MIT
# license: See README for details.
import Ophis.CmdLine
import Ophis.IR as IR
import Ophis.Frontend as FE
import Ophis.Errors as Err
@ -21,6 +22,14 @@ def reset():
currentcharmap = basecharmap
def pragmaOutfile(ppt, line, result):
"Sets the output file if it hasn't already been set"
filename = line.expect("STRING").value
line.expect("EOL")
if type(filename) == str and Ophis.CmdLine.outfile is None:
Ophis.CmdLine.outfile = filename
def pragmaInclude(ppt, line, result):
"Includes a source file"
filename = line.expect("STRING").value
@ -46,7 +55,7 @@ def pragmaIncbin(ppt, line, result):
line.expect("EOL")
if type(filename) == str:
try:
f = file(filename, "rb")
f = file(os.path.join(FE.context_directory, filename), "rb")
bytes = f.read()
f.close()
except IOError:
@ -82,7 +91,7 @@ def pragmaCharmapbin(ppt, line, result):
line.expect("EOL")
if type(filename) == str:
try:
f = file(filename, "rb")
f = file(os.path.join(FE.context_directory, filename), "rb")
bytes = f.read()
f.close()
except IOError:

View File

@ -8,6 +8,7 @@ import Ophis.IR as IR
import Ophis.CmdLine as Cmd
import sys
import os
import os.path
# Copyright 2002-2012 Michael C. Martin and additional contributors.
# You may use, modify, and distribute this file under the MIT
@ -346,9 +347,14 @@ def parse_line(ppt, lexemelist):
return IR.SequenceNode(ppt, result)
context_directory = None
def parse_file(ppt, filename):
"Loads an Ophis source file, and returns an IR list."
global context_directory
Err.currentpoint = ppt
old_context = context_directory
if Cmd.print_loaded_files:
if filename != '-':
print>>sys.stderr, "Loading " + filename
@ -356,18 +362,24 @@ def parse_file(ppt, filename):
print>>sys.stderr, "Loading from standard input"
try:
if filename != '-':
if context_directory is not None:
filename = os.path.join(context_directory, filename)
f = file(filename)
linelist = f.readlines()
f.close()
context_directory = os.path.abspath(os.path.dirname(filename))
else:
context_directory = os.getcwd()
linelist = sys.stdin.readlines()
pptlist = ["%s:%d" % (filename, i + 1) for i in range(len(linelist))]
lexlist = map(lex, pptlist, linelist)
IRlist = map(parse_line, pptlist, lexlist)
IRlist = [node for node in IRlist if node is not IR.NullNode]
context_directory = old_context
return IR.SequenceNode(ppt, IRlist)
except IOError:
Err.log("Could not read " + filename)
context_directory = old_context
return IR.NullNode

View File

@ -18,10 +18,10 @@ import Ophis.CmdLine
import Ophis.Opcodes
def run_all(infiles, outfile):
def run_all():
"Transforms the source infiles to a binary outfile."
Err.count = 0
z = Ophis.Frontend.parse(infiles)
z = Ophis.Frontend.parse(Ophis.CmdLine.infiles)
env = Ophis.Environment.Environment()
m = Ophis.Passes.ExpandMacros()
@ -55,6 +55,7 @@ def run_all(infiles, outfile):
if Err.count == 0:
try:
outfile = Ophis.CmdLine.outfile
if outfile == '-':
output = sys.stdout
elif outfile is None:
@ -81,7 +82,7 @@ def run_ophis(args):
Ophis.Opcodes.opcodes.update(Ophis.Opcodes.c02extensions)
Ophis.CorePragmas.reset()
run_all(Ophis.CmdLine.infiles, Ophis.CmdLine.outfile)
run_all()
if __name__ == '__main__':

View File

@ -1,19 +1,19 @@
#!/bin/bash
../bin/ophis -q --no-warn testbase.oph a.bin
diff -q testbase.bin a.bin
../bin/ophis -q --no-warn testdata.oph a.bin
diff -q testdata.bin a.bin
../bin/ophis -q --no-warn longbranch_ref.oph a_ref.bin
../bin/ophis -q --no-warn testbase.oph
diff -q testbase.bin ophis.bin
../bin/ophis -q --no-warn testdata.oph
diff -q testdata.bin ophis.bin
../bin/ophis -q --no-warn longbranch_ref.oph -o a_ref.bin
diff -q longbranch.bin a_ref.bin
../bin/ophis -q --no-warn longbranch.oph a.bin
diff -q longbranch.bin a.bin
../bin/ophis -cq --no-warn test65c02.oph a.bin
diff -q test65c02.bin a.bin
../bin/ophis -uq --no-warn test6510.oph a.bin
diff -q test6510.bin a.bin
../bin/ophis -cq --no-warn branch_c02_ref.oph a_ref.bin
../bin/ophis -q --no-warn longbranch.oph
diff -q longbranch.bin ophis.bin
../bin/ophis -cq --no-warn test65c02.oph
diff -q test65c02.bin ophis.bin
../bin/ophis -uq --no-warn test6510.oph
diff -q test6510.bin ophis.bin
../bin/ophis -cq --no-warn branch_c02_ref.oph -o a_ref.bin
diff -q branch_c02.bin a_ref.bin
../bin/ophis -cq --no-warn branch_c02.oph a.bin
diff -q branch_c02.bin a_ref.bin
rm -f a_ref.bin a.bin
../bin/ophis -cq --no-warn branch_c02.oph
diff -q branch_c02.bin ophis.bin
rm -f a_ref.bin ophis.bin