-
Notifications
You must be signed in to change notification settings - Fork 0
/
glox.scm
executable file
·62 lines (49 loc) · 1.65 KB
/
glox.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env -S guile -e main -s
!#
;; run with this command
;; GUILE_LOAD_PATH=. ./glox.scm
;; Usage String and Text Based Procedures with Ports
(use-modules (ice-9 textual-ports))
;; Define Modules
(use-modules (GuileLox error))
(use-modules (GuileLox token))
(use-modules (GuileLox token-type))
(use-modules (GuileLox scanner))
;; (use-modules (GuileLox scan))
;; Represents STDIN when promptin user for input, this is a Guile Port
(define STDIN (current-input-port))
(define STDOUT (current-output-port))
(define (run source)
(if (string? source)
(display "Is a string!\n")
(display "Is Not A String!\n"))
(display "Executing: \n")
(display source)
(newline)
;; open-input-string is a function that turns a string into a port
(display (scan-tokens (open-input-string source)))
(newline))
(define (run-file path)
(let ((fileport (open-file path "r")))
(if (and (not lox-error?) (port? fileport))
(run (get-string-all fileport))
(display "Error: Something is wrong in 'run-file'\n"))))
(define read-user (lambda (port)
(begin (display "> ")
(get-line port))))
(define (prompt stop?)
(if stop?
-1
(let ((user-input (read-user STDIN)))
(if (string=? "" user-input)
(prompt #f)
(begin (run user-input)
(set! lox-error? #f)
(prompt #f))))))
(define (run-prompt)
(prompt #f))
(define (main args)
(let ((num_args (length args)))
(cond ((> num_args 2) (begin (display "Usage: guilelox [script]\n")))
((= num_args 2) (run-file (list-ref args 1)))
(else (run-prompt)))))