Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #109 from MoeOrganization/prakashk/cmd-line-options
Browse files Browse the repository at this point in the history
Few command line options
  • Loading branch information
Stevan Little committed May 10, 2013
2 parents c14b1de + affe47a commit 390f2b5
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 9 deletions.
61 changes: 56 additions & 5 deletions src/main/scala/org/moe/Moe.scala
Expand Up @@ -22,6 +22,16 @@ object Moe {
options.addOption("w", false, "enable many useful warnings")
options.addOption("d", false, "debug mode")

options.addOption("n", false, """assume "while (<>) { ... }" loop around program""")
options.addOption("p", false, """assume loop like -n but print line also""")
options.addOption("a", false, """autosplit mode with -n or -p (splits $_ into @F)""")
// options.addOption("F", true, """split() pattern for -a switch""")

val f = new Option("F", "split() pattern for -a switch")
f.setArgs(1)
f.setArgName("pattern")
options.addOption(f)

val e = new Option("e", "code to evaluate")
e.setArgs(1)
e.setArgName("program")
Expand Down Expand Up @@ -90,25 +100,66 @@ object Moe {
)
)

def wrapCode(code: String) = {
if (cmd.hasOption("n") || cmd.hasOption("p")) {

// TODO: should support regex pattern for -F option
val autosplitCode =
if (cmd.hasOption("a")) {
val split_pattern = if (cmd.hasOption("F")) cmd.getOptionValue("F") else " "
s"""my @F = $$_.split("$split_pattern");\n"""
}
else ""

val printOutputCode = if (cmd.hasOption("p")) "; say $_;" else ""

val wrapped =
s"""
my $$__RUNNING_LINE_COUNT__ = 0;
for $$ARGV (@ARGV) {
my $$__ARGV_IO__ = IO.new($$ARGV);
my $$__LINE__ = 0;
while (!($$__ARGV_IO__.eof)) {
my $$_ = $$__ARGV_IO__.readline;
if (!($$__ARGV_IO__.eof)) {
$$__LINE__ = $$__RUNNING_LINE_COUNT__ + $$__ARGV_IO__.input_line_number;
$autosplitCode
## BEGIN INPUT CODE ##
${code.replaceAll("\\$\\.", "\\$__LINE__")}
## END INPUT CODE ##
$printOutputCode
}
}
$$__RUNNING_LINE_COUNT__ = $$__RUNNING_LINE_COUNT__ + $$__LINE__;
}
"""
if (runtime.isDebuggingOn) println("---\n" + wrapped + "\n---")
wrapped
}
else
code
}

val rest: List[String] = cmd.getArgs().toList

if (cmd.hasOption("e")) {
val code: String = cmd.getOptionValue("e")
if (!rest.isEmpty)
if (!rest.isEmpty) {
setupArgv(rest)
}
REPL.evalLine(
interpreter,
runtime,
code,
wrapCode(code),
Map("printOutput" -> false, "dumpAST" -> dumpAST, "printParserErrors" -> true)
)
return
}
else {
def evalProgram (path: String) = REPL.evalLine(
interpreter,
runtime,
Source.fromFile(path).mkString,
interpreter,
runtime,
wrapCode(Source.fromFile(path).mkString),
Map("printOutput" -> false, "dumpAST" -> dumpAST, "printParserErrors" -> true)
)

Expand Down
18 changes: 18 additions & 0 deletions src/main/scala/org/moe/runtime/builtins/IOClass.scala
Expand Up @@ -143,6 +143,24 @@ object IOClass {
)
)

ioClass.addMethod(
new MoeMethod(
"input_line_number",
new MoeSignature(),
env,
(e) => self(e).currentLineNumber(r)
)
)

ioClass.addMethod(
new MoeMethod(
"eof",
new MoeSignature(),
env,
(e) => self(e).atEndOfFile(r)
)
)

/**
* NOTE:
* This list needs some work, I have been punting on
Expand Down
26 changes: 22 additions & 4 deletions src/main/scala/org/moe/runtime/nativeobjects/MoeIOObject.scala
Expand Up @@ -16,10 +16,11 @@ class MoeIOObject(
v: java.io.File, t : Option[MoeType] = None
) extends MoeNativeObject[java.io.File](v, t) {

private lazy val reader = new java.io.BufferedReader(new java.io.FileReader(getNativeValue))
private lazy val reader = new java.io.LineNumberReader(new java.io.BufferedReader(new java.io.FileReader(getNativeValue)))
private lazy val writer = new java.io.FileWriter(getNativeValue)

private def file = getNativeValue
private var isAtEOF = false

// runtime methods

Expand All @@ -45,15 +46,28 @@ class MoeIOObject(

def readline (r: MoeRuntime): MoeStrObject = {
val line = reader.readLine()
r.NativeObjects.getStr(if (line == null) "" else line)
r.NativeObjects.getStr(
if (line == null) {
isAtEOF = true
""
}
else
line
)
}

def readlines (r: MoeRuntime): MoeArrayObject = r.NativeObjects.getArray(
Source.fromFile(file).getLines.toList.map(r.NativeObjects.getStr(_)):_*
{
Source.fromFile(file).getLines.toList.map(r.NativeObjects.getStr(_)):_*
}
)

def slurp (r: MoeRuntime): MoeStrObject = r.NativeObjects.getStr(
Source.fromFile(file).mkString
{
val slurped = Source.fromFile(file).mkString
isAtEOF = true
slurped
}
)

def close (r: MoeRuntime): MoeUndefObject = {
Expand All @@ -62,6 +76,10 @@ class MoeIOObject(
r.NativeObjects.getUndef
}

def currentLineNumber (r: MoeRuntime): MoeIntObject = r.NativeObjects.getInt( reader.getLineNumber )

def atEndOfFile (r: MoeRuntime): MoeBoolObject = r.NativeObjects.getBool( isAtEOF )

// MoeNativeObject overrides

override def copy = new MoeIOObject(
Expand Down

0 comments on commit 390f2b5

Please sign in to comment.