166 lines
5.5 KiB
Plaintext
166 lines
5.5 KiB
Plaintext
|
{{
|
||
|
Lex
|
||
|
|
||
|
usage: lex filename [options]
|
||
|
|
||
|
Reads filename.spn on SD card, writes tokens to filename.tok.
|
||
|
|
||
|
}}
|
||
|
con
|
||
|
_clkmode = xtal1 + pll8x
|
||
|
_xinfreq = 10_000_000
|
||
|
|
||
|
tvPin = 24
|
||
|
sdPin = 16
|
||
|
|
||
|
obj
|
||
|
term: "isxtv"
|
||
|
token: "tokenizr"
|
||
|
kw: "keywords"
|
||
|
fs: "sxfile" ' used for writing .tok file
|
||
|
str: "stringx"
|
||
|
bt: "bintree"
|
||
|
|
||
|
pub Main | err, p
|
||
|
err := \Try
|
||
|
if err
|
||
|
if err > 0
|
||
|
term.str( err )
|
||
|
else
|
||
|
term.str( string("Error ") )
|
||
|
term.dec( err )
|
||
|
term.out( 13 )
|
||
|
term.dec( token.LineNumber )
|
||
|
term.out( "," )
|
||
|
term.dec( token.Column + 1 )
|
||
|
term.out( " " )
|
||
|
term.out( "'" )
|
||
|
term.str( pTokenText )
|
||
|
term.out( "'" )
|
||
|
term.out( 13 )
|
||
|
fs.Close
|
||
|
fs.Open( string("sphinx.bin"), "R" )
|
||
|
else
|
||
|
fs.Close
|
||
|
if compile
|
||
|
fs.Open( string("codegen.bin"), "R" )
|
||
|
else
|
||
|
fs.Open( string("sphinx.bin"), "R" )
|
||
|
bt.Stop
|
||
|
fs.Execute( 0 )
|
||
|
|
||
|
var
|
||
|
long pTokenText
|
||
|
|
||
|
pri Try
|
||
|
bt.Init( 0, 0 )
|
||
|
ProcessCommandLine
|
||
|
if verbosity => 1
|
||
|
term.str( string("lex 090627", 13) )
|
||
|
Start
|
||
|
|
||
|
pri ProcessCommandLine | nArgs, l
|
||
|
fs.Open( string("args.d8a"), "R" )
|
||
|
nArgs := fs.ReadByte
|
||
|
ifnot nArgs--
|
||
|
abort string("usage: lex spinfile [options]")
|
||
|
fs.ReadStringUpperCase( @spinFilename, MAXFILENAMELENGTH )
|
||
|
|
||
|
l := strsize( @spinFilename )
|
||
|
if spinFilename[l-4] == "." and spinFilename[l-3] == "S" and spinFilename[l-2] == "P" and spinFilename[l-1] == "N"
|
||
|
spinFilename[l-4]~
|
||
|
if strsize( @spinFilename ) > 8
|
||
|
term.str( @spinFilename )
|
||
|
abort string(" -- filename too long")
|
||
|
str.Copy( @outputFilename, @spinFilename )
|
||
|
str.Append( @spinFilename, string(".SPN") )
|
||
|
str.Append( @outputFilename, string(".TOK") )
|
||
|
|
||
|
' Process command line arguments
|
||
|
|
||
|
repeat while nArgs--
|
||
|
fs.ReadStringUpperCase( @stringBuffer, MAXSTRINGBUFFERLENGTH )
|
||
|
if stringBuffer[0] == "-"
|
||
|
stringBuffer[0] := "/"
|
||
|
if strcomp( @stringBuffer, string("/C") )
|
||
|
compile~~
|
||
|
elseif strcomp( @stringBuffer, string("/L") )
|
||
|
compile~~
|
||
|
elseif strcomp( @stringBuffer, string("/V") )
|
||
|
ifnot nArgs--
|
||
|
abort string("/V must be followed by a number")
|
||
|
verbosity := fs.ReadNumber
|
||
|
'else ignore
|
||
|
|
||
|
fs.Close
|
||
|
|
||
|
con
|
||
|
MAXFILENAMELENGTH = 8 + 1 + 3 ' 8.3
|
||
|
MAXSTRINGBUFFERLENGTH = 32
|
||
|
|
||
|
var
|
||
|
byte spinFilename[MAXFILENAMELENGTH+1] ' input .spi file
|
||
|
byte outputFilename[MAXFILENAMELENGTH+1] ' output .tok file
|
||
|
byte stringBuffer[MAXSTRINGBUFFERLENGTH+1] ' temp string buffer
|
||
|
|
||
|
dat
|
||
|
verbosity byte 0 ' set by /V option
|
||
|
compile byte 0 ' set by /C or /L: if non-zero, run compile.bin automatically.
|
||
|
type long 0
|
||
|
lineNum word 0
|
||
|
column word 0
|
||
|
|
||
|
pri Start | v
|
||
|
|
||
|
kw.Init
|
||
|
pTokenText := token.GetPText
|
||
|
|
||
|
if verbosity => 2
|
||
|
term.str( string("Reading ") )
|
||
|
term.str( @spinFilename )
|
||
|
term.out( 13 )
|
||
|
token.Open( @spinFilename )
|
||
|
|
||
|
if verbosity => 2
|
||
|
term.str( string("Writing ") )
|
||
|
term.str( @outputFilename )
|
||
|
term.out( 13 )
|
||
|
fs.Open( @outputFilename, "W" )
|
||
|
|
||
|
repeat
|
||
|
type := token.Type
|
||
|
lineNum := token.LineNumber
|
||
|
column := token.Column
|
||
|
fs.Write( @type, 8 ) ' long + word + word
|
||
|
|
||
|
if type == kw#kINTLITERAL or type == kw#kFLOATLITERAL
|
||
|
v := token.Value
|
||
|
fs.Write( @v, 4 )
|
||
|
else
|
||
|
fs.Write( pTokenText, strsize(pTokenText) + 1 )
|
||
|
|
||
|
if token.Type == kw#kEOF
|
||
|
quit
|
||
|
token.Advance
|
||
|
token.Close
|
||
|
fs.Close
|
||
|
|
||
|
{{
|
||
|
Copyright (c) 2009 Michael Park
|
||
|
+------------------------------------------------------------------------------------------------------------------------------+
|
||
|
| TERMS OF USE: MIT License |
|
||
|
+------------------------------------------------------------------------------------------------------------------------------+
|
||
|
|Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation |
|
||
|
|files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, |
|
||
|
|modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software|
|
||
|
|is furnished to do so, subject to the following conditions: |
|
||
|
| |
|
||
|
|The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.|
|
||
|
| |
|
||
|
|THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
|
||
|
|WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
|
||
|
|COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
|
||
|
|ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
||
|
+------------------------------------------------------------------------------------------------------------------------------+
|
||
|
}}
|
||
|
|