From aaba249304b184e12e2445ab22d66df1f39a51a5 Mon Sep 17 00:00:00 2001 From: Joel Martin Date: Sat, 15 Nov 2014 23:51:59 -0600 Subject: [PATCH] VB.Net, C#: fix cmd line arg handling with --raw --- Makefile | 2 +- cs/core.cs | 5 ++--- cs/step2_eval.cs | 2 +- cs/step6_file.cs | 12 ++++++------ cs/step7_quote.cs | 12 ++++++------ cs/step8_macros.cs | 12 ++++++------ cs/step9_try.cs | 14 ++++++-------- cs/stepA_interop.cs | 12 ++++++------ cs/types.cs | 10 +++++----- vb/step0_repl.vb | 4 ++-- vb/step1_read_print.vb | 4 ++-- vb/step2_eval.vb | 4 ++-- vb/step3_env.vb | 4 ++-- vb/step4_if_fn_do.vb | 4 ++-- vb/step5_tco.vb | 4 ++-- vb/step6_file.vb | 16 ++++++++-------- vb/step7_quote.vb | 16 ++++++++-------- vb/step8_macros.vb | 16 ++++++++-------- vb/step9_try.vb | 14 ++++++-------- vb/stepA_interop.vb | 12 ++++++------ 20 files changed, 87 insertions(+), 92 deletions(-) diff --git a/Makefile b/Makefile index c709456b..0a01e01f 100644 --- a/Makefile +++ b/Makefile @@ -89,7 +89,7 @@ vb_RUNSTEP = mono ../$(2) --raw $(3) # Extra options to pass to runtest.py cs_TEST_OPTS = --redirect -mal_TEST_OPTS = --start-timeout 60 --test-timeout 120 +mal_TEST_OPTS = --redirect --start-timeout 60 --test-timeout 120 vb_TEST_OPTS = --redirect diff --git a/cs/core.cs b/cs/core.cs index 9cfe9894..8bbf6beb 100644 --- a/cs/core.cs +++ b/cs/core.cs @@ -38,8 +38,7 @@ namespace Mal { // Number functions static MalFunc time_ms = new MalFunc( - a => new MalInt((int)( - DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond))); + a => new MalInt(DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond)); // String functions static public MalFunc pr_str = new MalFunc( @@ -160,7 +159,7 @@ namespace Mal { }); static MalFunc nth = new MalFunc( - a => ((MalList)a[0])[ ((MalInt)a[1]).getValue() ]); + a => ((MalList)a[0])[ (int)((MalInt)a[1]).getValue() ]); static MalFunc first = new MalFunc( a => ((MalList)a[0])[0]); diff --git a/cs/step2_eval.cs b/cs/step2_eval.cs index 0c7db693..1e3866eb 100644 --- a/cs/step2_eval.cs +++ b/cs/step2_eval.cs @@ -12,7 +12,7 @@ using MalHashMap = Mal.types.MalHashMap; using MalFunc = Mal.types.MalFunc; namespace Mal { - class step1_eval { + class step2_eval { // read static MalVal READ(string str) { return reader.read_str(str); diff --git a/cs/step6_file.cs b/cs/step6_file.cs index ab7a4260..c8859224 100644 --- a/cs/step6_file.cs +++ b/cs/step6_file.cs @@ -141,8 +141,13 @@ namespace Mal { repl_env.set(entry.Key, entry.Value); } repl_env.set("eval", new MalFunc(a => EVAL(a[0], repl_env))); + int fileIdx = 1; + if (args.Length > 0 && args[0] == "--raw") { + Mal.readline.mode = Mal.readline.Mode.Raw; + fileIdx = 2; + } MalList _argv = new MalList(); - for (int i=1; i < args.Length; i++) { + for (int i=fileIdx; i < args.Length; i++) { _argv.conj_BANG(new MalString(args[i])); } repl_env.set("*ARGV*", _argv); @@ -151,11 +156,6 @@ namespace Mal { RE("(def! not (fn* (a) (if a false true)))"); RE("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"); - int fileIdx = 0; - if (args.Length > 0 && args[0] == "--raw") { - Mal.readline.mode = Mal.readline.Mode.Raw; - fileIdx = 1; - } if (args.Length > fileIdx) { RE("(load-file \"" + args[fileIdx] + "\")"); return; diff --git a/cs/step7_quote.cs b/cs/step7_quote.cs index 7c4cf111..c25498f7 100644 --- a/cs/step7_quote.cs +++ b/cs/step7_quote.cs @@ -173,8 +173,13 @@ namespace Mal { repl_env.set(entry.Key, entry.Value); } repl_env.set("eval", new MalFunc(a => EVAL(a[0], repl_env))); + int fileIdx = 1; + if (args.Length > 0 && args[0] == "--raw") { + Mal.readline.mode = Mal.readline.Mode.Raw; + fileIdx = 2; + } MalList _argv = new MalList(); - for (int i=1; i < args.Length; i++) { + for (int i=fileIdx; i < args.Length; i++) { _argv.conj_BANG(new MalString(args[i])); } repl_env.set("*ARGV*", _argv); @@ -183,11 +188,6 @@ namespace Mal { RE("(def! not (fn* (a) (if a false true)))"); RE("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"); - int fileIdx = 0; - if (args.Length > 0 && args[0] == "--raw") { - Mal.readline.mode = Mal.readline.Mode.Raw; - fileIdx = 1; - } if (args.Length > fileIdx) { RE("(load-file \"" + args[fileIdx] + "\")"); return; diff --git a/cs/step8_macros.cs b/cs/step8_macros.cs index 1aaa1fa5..ccc0242b 100644 --- a/cs/step8_macros.cs +++ b/cs/step8_macros.cs @@ -210,8 +210,13 @@ namespace Mal { repl_env.set(entry.Key, entry.Value); } repl_env.set("eval", new MalFunc(a => EVAL(a[0], repl_env))); + int fileIdx = 1; + if (args.Length > 0 && args[0] == "--raw") { + Mal.readline.mode = Mal.readline.Mode.Raw; + fileIdx = 2; + } MalList _argv = new MalList(); - for (int i=1; i < args.Length; i++) { + for (int i=fileIdx; i < args.Length; i++) { _argv.conj_BANG(new MalString(args[i])); } repl_env.set("*ARGV*", _argv); @@ -222,11 +227,6 @@ namespace Mal { RE("(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))"); RE("(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))"); - int fileIdx = 0; - if (args.Length > 0 && args[0] == "--raw") { - Mal.readline.mode = Mal.readline.Mode.Raw; - fileIdx = 1; - } if (args.Length > fileIdx) { RE("(load-file \"" + args[fileIdx] + "\")"); return; diff --git a/cs/step9_try.cs b/cs/step9_try.cs index e5da607d..1d724d18 100644 --- a/cs/step9_try.cs +++ b/cs/step9_try.cs @@ -231,31 +231,29 @@ namespace Mal { repl_env.set(entry.Key, entry.Value); } repl_env.set("eval", new MalFunc(a => EVAL(a[0], repl_env))); + int fileIdx = 1; + if (args.Length > 0 && args[0] == "--raw") { + Mal.readline.mode = Mal.readline.Mode.Raw; + fileIdx = 2; + } MalList _argv = new MalList(); - for (int i=1; i < args.Length; i++) { + for (int i=fileIdx; i < args.Length; i++) { _argv.conj_BANG(new MalString(args[i])); } repl_env.set("*ARGV*", _argv); // core.mal: defined using the language itself - RE("(def! *host-language* \"c#\")"); RE("(def! not (fn* (a) (if a false true)))"); RE("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"); RE("(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))"); RE("(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))"); - int fileIdx = 0; - if (args.Length > 0 && args[0] == "--raw") { - Mal.readline.mode = Mal.readline.Mode.Raw; - fileIdx = 1; - } if (args.Length > fileIdx) { RE("(load-file \"" + args[fileIdx] + "\")"); return; } // repl loop - RE("(println (str \"Mal [\" *host-language* \"]\"))"); while (true) { string line; try { diff --git a/cs/stepA_interop.cs b/cs/stepA_interop.cs index d84eca26..f51f824a 100644 --- a/cs/stepA_interop.cs +++ b/cs/stepA_interop.cs @@ -231,8 +231,13 @@ namespace Mal { repl_env.set(entry.Key, entry.Value); } repl_env.set("eval", new MalFunc(a => EVAL(a[0], repl_env))); + int fileIdx = 1; + if (args.Length > 0 && args[0] == "--raw") { + Mal.readline.mode = Mal.readline.Mode.Raw; + fileIdx = 2; + } MalList _argv = new MalList(); - for (int i=1; i < args.Length; i++) { + for (int i=fileIdx; i < args.Length; i++) { _argv.conj_BANG(new MalString(args[i])); } repl_env.set("*ARGV*", _argv); @@ -244,11 +249,6 @@ namespace Mal { RE("(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))"); RE("(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))"); - int fileIdx = 0; - if (args.Length > 0 && args[0] == "--raw") { - Mal.readline.mode = Mal.readline.Mode.Raw; - fileIdx = 1; - } if (args.Length > fileIdx) { RE("(load-file \"" + args[fileIdx] + "\")"); return; diff --git a/cs/types.cs b/cs/types.cs index fd0b9fbf..c2f46c96 100644 --- a/cs/types.cs +++ b/cs/types.cs @@ -98,11 +98,11 @@ namespace Mal { static public MalConstant False = new MalConstant("false"); public class MalInt : MalVal { - int value; - public MalInt(int v) { value = v; } + Int64 value; + public MalInt(Int64 v) { value = v; } public new MalInt copy() { return this; } - public int getValue() { return value; } + public Int64 getValue() { return value; } public override string ToString() { return value.ToString(); } @@ -205,10 +205,10 @@ namespace Mal { public int size() { return value.Count; } public MalVal nth(int idx) { - return value.Count > 0 ? value[idx] : Nil; + return value.Count > idx ? value[idx] : Nil; } public MalVal this[int idx] { - get { return value.Count > 0 ? value[idx] : Nil; } + get { return value.Count > idx ? value[idx] : Nil; } } public MalList rest() { if (size() > 0) { diff --git a/vb/step0_repl.vb b/vb/step0_repl.vb index ab8fe5ee..f53f3785 100644 --- a/vb/step0_repl.vb +++ b/vb/step0_repl.vb @@ -2,7 +2,7 @@ Imports System Imports Mal Namespace Mal - class step0_repl + Class step0_repl ' read Shared Function READ(str As String) As String Return str @@ -39,5 +39,5 @@ Namespace Mal Loop While True Return 0 End function - end class + End Class End Namespace diff --git a/vb/step1_read_print.vb b/vb/step1_read_print.vb index f8b47af7..27349730 100644 --- a/vb/step1_read_print.vb +++ b/vb/step1_read_print.vb @@ -4,7 +4,7 @@ Imports Mal Imports MalVal = Mal.types.MalVal Namespace Mal - class step1_read_print + Class step1_read_print ' read Shared Function READ(str As String) As MalVal Return reader.read_str(str) @@ -55,5 +55,5 @@ Namespace Mal End Try Loop While True End function - end class + End Class End Namespace diff --git a/vb/step2_eval.vb b/vb/step2_eval.vb index 70bc5e37..6e45efe8 100644 --- a/vb/step2_eval.vb +++ b/vb/step2_eval.vb @@ -11,7 +11,7 @@ Imports MalHashMap = Mal.types.MalHashMap Imports MalFunc = Mal.types.MalFunc Namespace Mal - class step2_eval + Class step2_eval ' read Shared Function READ(str As String) As MalVal Return reader.read_str(str) @@ -130,5 +130,5 @@ Namespace Mal End Try Loop While True End function - end class + End Class End Namespace diff --git a/vb/step3_env.vb b/vb/step3_env.vb index 2877ef14..5793fdb1 100644 --- a/vb/step3_env.vb +++ b/vb/step3_env.vb @@ -12,7 +12,7 @@ Imports MalFunc = Mal.types.MalFunc Imports MalEnv = Mal.env.Env Namespace Mal - class step3_eval + Class step3_env ' read Shared Function READ(str As String) As MalVal Return reader.read_str(str) @@ -152,5 +152,5 @@ Namespace Mal End Try Loop While True End function - end class + End Class End Namespace diff --git a/vb/step4_if_fn_do.vb b/vb/step4_if_fn_do.vb index 2f5e16a6..4cf8721a 100644 --- a/vb/step4_if_fn_do.vb +++ b/vb/step4_if_fn_do.vb @@ -12,7 +12,7 @@ Imports MalFunc = Mal.types.MalFunc Imports MalEnv = Mal.env.Env Namespace Mal - class step4_if_fn_do + Class step4_if_fn_do ' read Shared Function READ(str As String) As MalVal Return reader.read_str(str) @@ -185,5 +185,5 @@ Namespace Mal End Try Loop While True End function - end class + End Class End Namespace diff --git a/vb/step5_tco.vb b/vb/step5_tco.vb index a379cf5a..3c1f51cd 100644 --- a/vb/step5_tco.vb +++ b/vb/step5_tco.vb @@ -12,7 +12,7 @@ Imports MalFunc = Mal.types.MalFunc Imports MalEnv = Mal.env.Env Namespace Mal - class step5_tco + Class step5_tco ' read Shared Function READ(str As String) As MalVal Return reader.read_str(str) @@ -194,5 +194,5 @@ Namespace Mal End Try Loop While True End function - end class + End Class End Namespace diff --git a/vb/step6_file.vb b/vb/step6_file.vb index 32cbe5bc..58f8cd24 100644 --- a/vb/step6_file.vb +++ b/vb/step6_file.vb @@ -13,7 +13,7 @@ Imports MalFunc = Mal.types.MalFunc Imports MalEnv = Mal.env.Env Namespace Mal - class step6_file + Class step6_file ' read Shared Function READ(str As String) As MalVal Return reader.read_str(str) @@ -169,8 +169,13 @@ Namespace Mal repl_env.do_set(entry.Key, entry.Value) Next repl_env.do_set("eval", new MalFunc(AddressOf do_eval)) + Dim fileIdx As Integer = 1 + If args.Length > 1 AndAlso args(1) = "--raw" Then + Mal.readline.SetMode(Mal.readline.Modes.Raw) + fileIdx = 2 + End If Dim argv As New MalList() - For i As Integer = 0 To args.Length()-1 + For i As Integer = fileIdx+1 To args.Length-1 argv.conj_BANG(new MalString(args(i))) Next repl_env.do_set("*ARGV*", argv) @@ -179,11 +184,6 @@ Namespace Mal REP("(def! not (fn* (a) (if a false true)))") REP("(def! load-file (fn* (f) (eval (read-string (str ""(do "" (slurp f) "")"")))))") - Dim fileIdx As Integer = 1 - If args.Length > 1 AndAlso args(1) = "--raw" Then - Mal.readline.SetMode(Mal.readline.Modes.Raw) - fileIdx = 2 - End If If args.Length > fileIdx Then REP("(load-file """ & args(fileIdx) & """)") return 0 @@ -212,5 +212,5 @@ Namespace Mal End Try Loop While True End function - end class + End Class End Namespace diff --git a/vb/step7_quote.vb b/vb/step7_quote.vb index d3b61747..3f2d614c 100644 --- a/vb/step7_quote.vb +++ b/vb/step7_quote.vb @@ -13,7 +13,7 @@ Imports MalFunc = Mal.types.MalFunc Imports MalEnv = Mal.env.Env Namespace Mal - class step7_quote + Class step7_quote ' read Shared Function READ(str As String) As MalVal Return reader.read_str(str) @@ -202,8 +202,13 @@ Namespace Mal repl_env.do_set(entry.Key, entry.Value) Next repl_env.do_set("eval", new MalFunc(AddressOf do_eval)) + Dim fileIdx As Integer = 1 + If args.Length > 1 AndAlso args(1) = "--raw" Then + Mal.readline.SetMode(Mal.readline.Modes.Raw) + fileIdx = 2 + End If Dim argv As New MalList() - For i As Integer = 0 To args.Length()-1 + For i As Integer = fileIdx+1 To args.Length-1 argv.conj_BANG(new MalString(args(i))) Next repl_env.do_set("*ARGV*", argv) @@ -212,11 +217,6 @@ Namespace Mal REP("(def! not (fn* (a) (if a false true)))") REP("(def! load-file (fn* (f) (eval (read-string (str ""(do "" (slurp f) "")"")))))") - Dim fileIdx As Integer = 1 - If args.Length > 1 AndAlso args(1) = "--raw" Then - Mal.readline.SetMode(Mal.readline.Modes.Raw) - fileIdx = 2 - End If If args.Length > fileIdx Then REP("(load-file """ & args(fileIdx) & """)") return 0 @@ -245,5 +245,5 @@ Namespace Mal End Try Loop While True End function - end class + End Class End Namespace diff --git a/vb/step8_macros.vb b/vb/step8_macros.vb index 5a5fc774..1a7f2110 100644 --- a/vb/step8_macros.vb +++ b/vb/step8_macros.vb @@ -13,7 +13,7 @@ Imports MalFunc = Mal.types.MalFunc Imports MalEnv = Mal.env.Env Namespace Mal - class step8_macros + Class step8_macros ' read Shared Function READ(str As String) As MalVal Return reader.read_str(str) @@ -240,8 +240,13 @@ Namespace Mal repl_env.do_set(entry.Key, entry.Value) Next repl_env.do_set("eval", new MalFunc(AddressOf do_eval)) + Dim fileIdx As Integer = 1 + If args.Length > 1 AndAlso args(1) = "--raw" Then + Mal.readline.SetMode(Mal.readline.Modes.Raw) + fileIdx = 2 + End If Dim argv As New MalList() - For i As Integer = 0 To args.Length()-1 + For i As Integer = fileIdx+1 To args.Length-1 argv.conj_BANG(new MalString(args(i))) Next repl_env.do_set("*ARGV*", argv) @@ -252,11 +257,6 @@ Namespace Mal REP("(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw ""odd number of forms to cond"")) (cons 'cond (rest (rest xs)))))))") REP("(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))") - Dim fileIdx As Integer = 1 - If args.Length > 1 AndAlso args(1) = "--raw" Then - Mal.readline.SetMode(Mal.readline.Modes.Raw) - fileIdx = 2 - End If If args.Length > fileIdx Then REP("(load-file """ & args(fileIdx) & """)") return 0 @@ -285,5 +285,5 @@ Namespace Mal End Try Loop While True End function - end class + End Class End Namespace diff --git a/vb/step9_try.vb b/vb/step9_try.vb index 0b888b71..5bf600de 100644 --- a/vb/step9_try.vb +++ b/vb/step9_try.vb @@ -263,24 +263,23 @@ Namespace Mal repl_env.do_set(entry.Key, entry.Value) Next repl_env.do_set("eval", new MalFunc(AddressOf do_eval)) + Dim fileIdx As Integer = 1 + If args.Length > 1 AndAlso args(1) = "--raw" Then + Mal.readline.SetMode(Mal.readline.Modes.Raw) + fileIdx = 2 + End If Dim argv As New MalList() - For i As Integer = 0 To args.Length()-1 + For i As Integer = fileIdx+1 To args.Length-1 argv.conj_BANG(new MalString(args(i))) Next repl_env.do_set("*ARGV*", argv) ' core.mal: defined using the language itself - REP("(def! *host-language* ""VB.NET"")") REP("(def! not (fn* (a) (if a false true)))") REP("(def! load-file (fn* (f) (eval (read-string (str ""(do "" (slurp f) "")"")))))") REP("(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw ""odd number of forms to cond"")) (cons 'cond (rest (rest xs)))))))") REP("(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))") - Dim fileIdx As Integer = 1 - If args.Length > 1 AndAlso args(1) = "--raw" Then - Mal.readline.SetMode(Mal.readline.Modes.Raw) - fileIdx = 2 - End If If args.Length > fileIdx Then REP("(load-file """ & args(fileIdx) & """)") return 0 @@ -288,7 +287,6 @@ Namespace Mal ' repl loop Dim line As String - REP("(println (str ""Mal ["" *host-language* ""]""))") Do Try line = Mal.readline.Readline("user> ") diff --git a/vb/stepA_interop.vb b/vb/stepA_interop.vb index 713954d7..01702fa4 100644 --- a/vb/stepA_interop.vb +++ b/vb/stepA_interop.vb @@ -263,8 +263,13 @@ Namespace Mal repl_env.do_set(entry.Key, entry.Value) Next repl_env.do_set("eval", new MalFunc(AddressOf do_eval)) + Dim fileIdx As Integer = 1 + If args.Length > 1 AndAlso args(1) = "--raw" Then + Mal.readline.SetMode(Mal.readline.Modes.Raw) + fileIdx = 2 + End If Dim argv As New MalList() - For i As Integer = 0 To args.Length()-1 + For i As Integer = fileIdx+1 To args.Length-1 argv.conj_BANG(new MalString(args(i))) Next repl_env.do_set("*ARGV*", argv) @@ -276,11 +281,6 @@ Namespace Mal REP("(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw ""odd number of forms to cond"")) (cons 'cond (rest (rest xs)))))))") REP("(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))") - Dim fileIdx As Integer = 1 - If args.Length > 1 AndAlso args(1) = "--raw" Then - Mal.readline.SetMode(Mal.readline.Modes.Raw) - fileIdx = 2 - End If If args.Length > fileIdx Then REP("(load-file """ & args(fileIdx) & """)") return 0 -- 2.20.1