Skip to content

Commit fe8b60d

Browse files
committedAug 22, 2024·
WIP adding argv. #96
1 parent c1de7b4 commit fe8b60d

File tree

5 files changed

+44
-8
lines changed

5 files changed

+44
-8
lines changed
 

‎examples/galax.l1

+3-2
Original file line numberDiff line numberDiff line change
@@ -174,5 +174,6 @@
174174
darlena arlinda arlen junita margeret anette selina wilma reena
175175
tessa delma))
176176

177-
(dotimes 10
178-
(printl (itsalive (randchoice all-names))))
177+
(let ((n (number (or (caddr ARGV) 10))))
178+
(dotimes n
179+
(printl (itsalive (randchoice all-names)))))

‎lisp/builtin.go

+20
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ func InitGlobals() Env {
3131
globals.Set("BQUOTE", Atom{"`"})
3232
globals.Set("DQUOTE", Atom{"\""})
3333
globals.Set("UPLINE", Atom{"\033[F"})
34+
// Form up ARGV:
35+
var args []Sexpr
36+
for _, arg := range os.Args {
37+
args = append(args, Atom{arg})
38+
}
39+
globals.Set("ARGV", mkListAsConsWithCdr(args, Nil))
3440
return globals
3541
}
3642

@@ -882,6 +888,20 @@ func init() {
882888
return Nil, nil
883889
},
884890
},
891+
"number": {
892+
Name: "number",
893+
Doc: DOC("Cast an atom, probably from an external source, to a number. Mostly used for parsing ARGV."),
894+
FixedArity: 1,
895+
NAry: false,
896+
Args: LC(A("x")),
897+
Fn: func(args []Sexpr, _ *Env) (Sexpr, error) {
898+
if len(args) != 1 {
899+
return nil, baseError("number expects a single argument")
900+
}
901+
n := Num(args[0].String())
902+
return n, nil
903+
},
904+
},
885905
"print": {
886906
Name: "print",
887907
Doc: DOC("Print the arguments"),

‎lisp/builtin_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,19 @@ func TestListFromSemver(t *testing.T) {
6868
}
6969
}
7070
}
71+
72+
// Test that the number function returns the number it is given.
73+
// Primarily useful for handling argv values.
74+
func TestNumberTest(t *testing.T) {
75+
// Emulate setting of an argv value:
76+
env := mkEnv(nil)
77+
env.Set("h", Atom{"100"})
78+
// Check the function proper:
79+
result, err := eval(Cons(Atom{"number"}, Cons(Atom{"h"}, Nil)), &env)
80+
if err != nil {
81+
t.Errorf("eval(num 100) = %q", err)
82+
}
83+
if !result.Equal(Num(100)) {
84+
t.Errorf("eval(num 100) = %q", result)
85+
}
86+
}

‎lisp/examples.txt

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ l1 - a Lisp interpreter.
103103
not N 1 Return t if the argument is nil, () otherwise
104104
not= F 0+ Complement of = function
105105
nth F 2 Find the nth value of a list, starting from zero
106+
number N 1 Cast an atom, probably from an external source, to a number. Mostly used for parsing ARGV.
106107
number? N 1 Return true if the argument is a number, else ()
107108
odd? F 1 Return true if the supplied integer argument is odd
108109
or S 0+ Boolean or

‎main.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,10 @@ func main() {
104104

105105
files := flag.Args()
106106
if len(files) > 0 {
107-
for _, file := range files {
108-
err := lisp.LoadFile(&globals, file)
109-
if err != nil {
110-
fmt.Printf("ERROR:\n%v\n", err)
111-
os.Exit(1)
112-
}
107+
err := lisp.LoadFile(&globals, files[0])
108+
if err != nil {
109+
fmt.Printf("ERROR:\n%v\n", err)
110+
os.Exit(1)
113111
}
114112
return
115113
}

0 commit comments

Comments
 (0)
Please sign in to comment.