Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP adding argv. #96 #97

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading