diff --git a/platform/c64/demo/c64_hello.oph b/platform/c64/demo/c64_hello.oph index 4dd0f55..0b43fd7 100644 --- a/platform/c64/demo/c64_hello.oph +++ b/platform/c64/demo/c64_hello.oph @@ -1,6 +1,8 @@ .include "../c64_0.oph" .include "../c64kernal.oph" +.outfile "hello.prg" + ldy #$00 * lda text, y beq + diff --git a/platform/nes/demo/hello_ines.oph b/platform/nes/demo/hello_ines.oph index dfb4552..85c83de 100644 --- a/platform/nes/demo/hello_ines.oph +++ b/platform/nes/demo/hello_ines.oph @@ -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" diff --git a/platform/nes/demo/hello_unif.oph b/platform/nes/demo/hello_unif.oph index 11894b9..ca34e20 100644 --- a/platform/nes/demo/hello_unif.oph +++ b/platform/nes/demo/hello_unif.oph @@ -1,3 +1,4 @@ +.outfile "hello.unf" .byte "UNIF" .dword 7 .advance $20 diff --git a/platform/stella/demo/hi_stella.oph b/platform/stella/demo/hi_stella.oph index 34a229e..1d77aab 100644 --- a/platform/stella/demo/hi_stella.oph +++ b/platform/stella/demo/hi_stella.oph @@ -1,4 +1,5 @@ .require "../stella.oph" +.outfile "hi_stella.bin" .data .org $0080 diff --git a/src/Ophis/CorePragmas.py b/src/Ophis/CorePragmas.py index 7087785..215ade5 100644 --- a/src/Ophis/CorePragmas.py +++ b/src/Ophis/CorePragmas.py @@ -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: diff --git a/src/Ophis/Frontend.py b/src/Ophis/Frontend.py index 8bade2e..695e800 100644 --- a/src/Ophis/Frontend.py +++ b/src/Ophis/Frontend.py @@ -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 diff --git a/src/Ophis/Main.py b/src/Ophis/Main.py index 2513d23..20d401b 100644 --- a/src/Ophis/Main.py +++ b/src/Ophis/Main.py @@ -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__': diff --git a/tests/simpletest.sh b/tests/simpletest.sh index 8f36e1c..9277403 100755 --- a/tests/simpletest.sh +++ b/tests/simpletest.sh @@ -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