#!/usr/bin/env rexx /******************************************************************************/ /* */ /* rvm.rex - Compile a Rexx program to R-code and run it on the RVM */ /* ================================================================ */ /* */ /* Usage: rvm [-l | -c | -w] FILE [ARGUMENTS] */ /* rvm -e PROGRAM [ARGUMENTS] */ /* rvm -f */ /* rvm -p FILE */ /* rvm -s OPTION */ /* */ /* (no option) compile FILE to R-code and run it */ /* -l list the R-code on standard output, do not run it */ /* -c write the R-code to FILE.rcode, do not run it */ /* -w run, and write the R-code of each source compiled (FILE */ /* and its external routines) next to it, as a cache */ /* FILE.rcode run R-code already compiled */ /* -e PROGRAM run PROGRAM, a program given as one argument (a string), */ /* named INSTORE and with no .SYSCARGS, as rexx -e does */ /* */ /* A source whose R-code is next to it (x.rex, x.rcode), made from the same */ /* text by the same compiler (see CachedRCode in RVM.Machine), runs from that */ /* R-code, without compiling. -c writes it; so does -w. */ /* */ /* The options -f, -p and -s serve the JS RVM under Node, which cannot */ /* compile: */ /* */ /* -f compile INTERPRET data (or a line typed at an interactive */ /* trace pause) for an RVM that cannot compile (the JS RVM */ /* under Node), from standard input: */ /* FRAGMENT kind line (kind I or D; line: the clause's) */ /* name (the program's name) */ /* labels (hex, blank-separated) */ /* data (hex) */ /* and writes "OK" and the fragment's R-code (see */ /* RVM.Compiler~fragment), or, when the data does not parse, */ /* "ERROR code column clause" (the clause in error, hex, or */ /* "-" when the parser does not give it) and one line per */ /* insert (hex). */ /* -p FILE compile FILE (an external routine of the JS RVM): "OK" and */ /* its R-code, or when it does not parse "ERROR code line", */ /* the report line (hex) and one line per insert (hex). */ /* -s OPTION compile a source given at run time (Method~new, Routine~new */ /* for the JS RVM under Node), with the parser's OPTION */ /* (METHOD, or - for none), from */ /* standard input: its name (hex), its number of lines, and */ /* its lines (hex); writes what -p writes. */ /* */ /* The program always runs from the TEXT of its R-code: what the compiler */ /* writes is what the machine reads. Errors are reported as the native */ /* interpreter reports them. */ /* */ /* This file is part of the RVM, the Rexx Virtual Machine */ /* [See https://rexx.epbcn.com/rexx-parser/] */ /* */ /* Copyright (c) 2026 Josep Maria Blasco */ /* */ /* License: Apache License 2.0 (https://www.apache.org/licenses/LICENSE-2.0) */ /******************************************************************************/ Parse Arg line line = Strip( line, "L" ) If line == "-f" Then Signal Fragment If Word( line, 1 ) == "-p" Then Signal CompileFile If Word( line, 1 ) == "-s" Then Signal CompileLines mode = "RUN" If Word( line, 1 ) == "-l" Then Do; mode = "LIST"; line = SubWord( line, 2 ); End If Word( line, 1 ) == "-c" Then Do; mode = "WRITE"; line = SubWord( line, 2 ); End If Word( line, 1 ) == "-w" Then Do .RVM.Program~writeCache = 1 line = SubWord( line, 2 ) End -- rexx -e PROGRAM [ARGUMENTS]: a program in a string, named INSTORE (the -- string is the next word of the command line, .SYSCARGS; ooRexx gives -- such a program no .SYSCARGS). instore = .False If Word( line, 1 ) == "-e" Then Do a = .SysCArgs k = 0 If a~isA( .Array ) Then k = a~index( "-e" ) If k == .Nil | k == 0 | \ a~hasIndex( k + 1 ) Then Do .Error~lineOut("Usage: rvm -e PROGRAM [ARGUMENTS]") Exit 1 End instore = .True chunk = a[ k + 1 ] rest = "" Do i = k + 2 To a~items rest = rest a[ i ] End rest = Strip( rest, "L" ) fullPath = "INSTORE" Signal Parsed End If line == "" Then Do .Error~lineOut("Usage: rvm [-l | -c | -w] FILE [ARGUMENTS]") .Error~lineOut(" rvm -e PROGRAM [ARGUMENTS]") Exit 1 End If Left( line, 1 ) == '"' Then Do p = Pos( '"', line, 2 ) If p == 0 Then Do .Error~lineOut("Missing closing quote in the file name:" line) Exit 1 End file = SubStr( line, 2, p - 2 ) rest = SubStr( line, p + 1 ) End Else Parse Var line file rest rest = Strip( rest, "L" ) fullPath = .context~package~findProgram(file) If fullPath == .Nil Then Do .Error~lineOut("File '"file"' does not exist.") Exit 1 End Parsed: context = .Nil cached = .Nil If \ instore, \ fullPath~caselessEndsWith( ".rcode" ), mode == "RUN" Then Do chunk = CharIn( fullPath, 1, Chars( fullPath ) ) Call Stream fullPath, "C", "Close" cached = CachedRCode( fullPath, chunk ) End If fullPath~caselessEndsWith( ".rcode" ) | cached \== .Nil Then Do If cached \== .Nil Then Do rcode = cached source = SourceLines( chunk ) End Else Do rcode = CharIn( fullPath, 1, Chars( fullPath ) )~makeArray Call Stream fullPath, "C", "Close" source = .Nil End -- No parse: the argument table of the built-in functions (CheckArgs) -- is loaded here, with the parser's default options. .environment~Options.Experimental = 0 .environment~Options.CMS = 0 .environment~Options.Unicode = 0 Call LoadBIFInfo End Else Do If \ instore Then Do chunk = CharIn(fullPath, 1, Chars(fullPath)) Call Stream fullPath, "C", "Close" End source = SourceLines( chunk ) -- A syntax error in the program goes to ParseError; any other error of -- the parser, to Syntax Signal On User Rexx.Parser.Error Name ParseError Signal On Syntax parser = .Rexx.Parser~new(fullPath, source) Signal Off User Rexx.Parser.Error Signal Off Syntax unit = .RVM.Compiler~compile( parser~package, source, fullPath ) rcode = .RVM.RCode~write( unit, SourceHash( chunk ) ) If .RVM.Program~writeCache == 1, \ instore Then Call WriteCache fullPath, rcode If mode == "LIST" Then Do Do l Over rcode Say l End Exit 0 End If mode == "WRITE" Then Do out = CachePath( fullPath ) Call Stream out, "C", "Open Write Replace" Do l Over rcode Call LineOut out, l End Call Stream out, "C", "Close" Exit 0 End End program = .RVM.RCode~read( rcode ) program~path = fullPath program~source = source Signal On Syntax context = .RVM.Context~new If source \== .Nil Then context~sourceLines = source context~programName = fullPath If rest \== "" Then context~args = .Array~of( rest ) -- .SYSCARGS: the words of the command line after the program's name. If .SysCArgs~isA( .Array ), \ instore Then Do k = 1 Do While k <= .SysCArgs~items, WordPos( .SysCArgs[k], "-w" ) > 0 k += 1 End context~syscargs = .SysCArgs~section( k + 1 ) End If instore Then context~syscargs = .False .RVM.SystemHost~shareQueues -- started by a command of an RVM: its queues .RVM.Machine~run( program, context ) .RVM.SystemHost~endProgram code = context~exitCode If code~dataType("W") Then Exit code Exit 0 Syntax: co = Condition("O") .RVM.SystemHost~endProgram lines = .Nil If context \== .Nil Then lines = context~errorLines If lines == .Nil Then lines = .Array~of( .Array~of( 0, "" ) ) header = .Nil If context \== .Nil Then header = context~errorHeader Exit Report( BifCodeOf( co ), BifMessageOf( co ), lines, header, context ) -- A syntax error in the program (a Rexx.Parser.Error, thrown) ParseError: error = Condition("O")~additional line = ParseErrorLine( source, error ) Exit Report( error~code, error~message, - .Array~of( .Array~of( error~line, line ) ) ) -- rvm -f: INTERPRET data compiled apart (see the header). Fragment: Parse LineIn . kind clauseLine Parse LineIn name Parse LineIn labelsHex Parse LineIn dataHex data = X2C( dataHex ) labels = .Table~new Do w Over labelsHex~makeArray(" ") labels[ X2C( w ) ] = .RVM.LabelRef~new End source = .Array~of( data ) Signal On User Rexx.Parser.Error Name FragmentError parser = .Rexx.Parser~new( name, source, - .Array~of( .Array~of( "INTERPRET", labels ) ) ) package = parser~package Signal Off User Rexx.Parser.Error .environment~Options.Interpret = 0 .RVM.Fragments~register( package, source, clauseLine ) unit = .RVM.Compiler~fragment( package, source, name, kind ) Say "OK" Do l Over .RVM.RCode~write( unit ) Say l End Exit 0 -- rvm -p FILE: compile an external routine for the JS RVM (see the header). CompileFile: file = Strip( SubWord( line, 2 ) ) -- (Windows: rexx gets the command line as it is, so a name with blanks -- comes between the double quotes Node put around it.) If Length( file ) >= 2, Left( file, 1 ) == '"', Right( file, 1 ) == '"' Then file = SubStr( file, 2, Length( file ) - 2 ) -- A file that is not there is an error, not an empty program. If \ SysIsFile( file ) Then Do .Error~lineOut( "rvm -p: no such file:" file ) Exit 1 End text = CharIn( file, 1, Chars( file ) ) Call Stream file, "C", "Close" source = SourceLines( text ) Signal On User Rexx.Parser.Error Name CompileFileError parser = .Rexx.Parser~new( file, source ) package = parser~package Signal Off User Rexx.Parser.Error unit = .RVM.Compiler~compile( package, source, file ) Say "OK" Do l Over .RVM.RCode~write( unit, SourceHash( text ) ) Say l End Exit 0 -- rvm -s OPTION: a source compiled at run time (see the header). CompileLines: option = Word( line, 2 ) Parse LineIn nameHex name = X2C( nameHex ) Parse LineIn n source = .Array~new Do n Parse LineIn h source~append( X2C( h ) ) End Signal On User Rexx.Parser.Error Name CompileFileError options = .Array~new If option \== "-" Then options~append( .Array~of( option, 1 ) ) parser = .Rexx.Parser~new( name, source, options ) package = parser~package Signal Off User Rexx.Parser.Error unit = .RVM.Compiler~compile( package, source, name ) Say "OK" Do l Over .RVM.RCode~write( unit ) Say l End Exit 0 CompileFileError: error = Condition("O")~additional Say "ERROR" error~code error~line Say C2X( ParseErrorLine( source, error ) ) Do insert Over error~substitutions Say C2X( insert ) End Exit 0 FragmentError: error = Condition("O")~additional col = error~column If col == "" Then col = 0 clause = "-" If error~clause \== "" Then clause = C2X( error~clause ) Say "ERROR" error~code col clause Do insert Over error~substitutions Say C2X( insert ) End Exit 0 Report: Use Strict Arg code, message, lines, header = .Nil, ctx = .Nil Return ErrorReport( code, message, lines, header, fullPath, ctx ) ::Requires "parser.cls" ::Requires "modules/rvm/RVM.Compiler.cls" ::Requires "modules/rvm/RVM.Machine.cls" ::Requires "modules/rvm/RVM.BIFs.cls" ::Requires "parser/BIFs.cls"