Skip to content

Commit

Permalink
WIP adding argv. #96
Browse files Browse the repository at this point in the history
  • Loading branch information
eigenhombre committed Aug 22, 2024
1 parent c1de7b4 commit fe8b60d
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 8 deletions.
5 changes: 3 additions & 2 deletions examples/galax.l1
Original file line number Diff line number Diff line change
Expand Up @@ -174,5 +174,6 @@
darlena arlinda arlen junita margeret anette selina wilma reena
tessa delma))

(dotimes 10
(printl (itsalive (randchoice all-names))))
(let ((n (number (or (caddr ARGV) 10))))
(dotimes n
(printl (itsalive (randchoice all-names)))))
20 changes: 20 additions & 0 deletions lisp/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ func InitGlobals() Env {
globals.Set("BQUOTE", Atom{"`"})
globals.Set("DQUOTE", Atom{"\""})
globals.Set("UPLINE", Atom{"\033[F"})
// Form up ARGV:
var args []Sexpr
for _, arg := range os.Args {
args = append(args, Atom{arg})
}
globals.Set("ARGV", mkListAsConsWithCdr(args, Nil))
return globals
}

Expand Down Expand Up @@ -882,6 +888,20 @@ func init() {
return Nil, nil
},
},
"number": {
Name: "number",
Doc: DOC("Cast an atom, probably from an external source, to a number. Mostly used for parsing ARGV."),
FixedArity: 1,
NAry: false,
Args: LC(A("x")),
Fn: func(args []Sexpr, _ *Env) (Sexpr, error) {
if len(args) != 1 {
return nil, baseError("number expects a single argument")
}
n := Num(args[0].String())
return n, nil
},
},
"print": {
Name: "print",
Doc: DOC("Print the arguments"),
Expand Down
16 changes: 16 additions & 0 deletions lisp/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,19 @@ func TestListFromSemver(t *testing.T) {
}
}
}

// Test that the number function returns the number it is given.
// Primarily useful for handling argv values.
func TestNumberTest(t *testing.T) {
// Emulate setting of an argv value:
env := mkEnv(nil)
env.Set("h", Atom{"100"})
// Check the function proper:
result, err := eval(Cons(Atom{"number"}, Cons(Atom{"h"}, Nil)), &env)
if err != nil {
t.Errorf("eval(num 100) = %q", err)
}
if !result.Equal(Num(100)) {
t.Errorf("eval(num 100) = %q", result)
}
}
1 change: 1 addition & 0 deletions lisp/examples.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ l1 - a Lisp interpreter.
not N 1 Return t if the argument is nil, () otherwise
not= F 0+ Complement of = function
nth F 2 Find the nth value of a list, starting from zero
number N 1 Cast an atom, probably from an external source, to a number. Mostly used for parsing ARGV.
number? N 1 Return true if the argument is a number, else ()
odd? F 1 Return true if the supplied integer argument is odd
or S 0+ Boolean or
Expand Down
10 changes: 4 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,10 @@ func main() {

files := flag.Args()
if len(files) > 0 {
for _, file := range files {
err := lisp.LoadFile(&globals, file)
if err != nil {
fmt.Printf("ERROR:\n%v\n", err)
os.Exit(1)
}
err := lisp.LoadFile(&globals, files[0])
if err != nil {
fmt.Printf("ERROR:\n%v\n", err)
os.Exit(1)
}
return
}
Expand Down

0 comments on commit fe8b60d

Please sign in to comment.