phi-lang/compile.phi
2025-09-10 02:04:13 +02:00

94 lines
2.2 KiB
Plaintext

(let identChars "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890+-*/%&|=?!<>'_")
(fn tokenize (text) (do
(let text_len (call len text))
(let tokens ())
(let i 0)
(let line 1)
(loop (do
(if (>= i text_len) (break))
(let ch (call at text i))
(if (call contains " \t\r\n" ch) (do
(call println "line = %, ch = '%'" line ch)
(if (== ch "\n") (do
(+= line 1)
))
(+= i 1)
) (if (call slice_eq text i "//") (do
(loop (do
if (or (>= i text_len) (== (call at text i) "\n") (do
(break)
))
(+= i 1)
))
) (if (call contains "()" ch) (do
(call push tokens (ch line))
(+= i 1)
) (if (== ch "\"") (do
(+= i 1)
) (if (call contains identChars ch) (do
(let value "")
(loop (do
(= ch (call at text i))
(if (or (>= i text_len) (not (call contains identChars ch))) (do
(break)
))
(call push value ch)
(+= i 1)
))
(call push tokens ("ident" line value))
) (do
(call println "illegal char '%'" ch)
(+= i 1)
))))))
))
(return tokens)
))
(fn contains (text ch) (do
(let text_len (call len text))
(let i 0)
(loop (do
(if (>= i text_len) (break))
(if (== (call at text i) ch) (do
(return true)
))
(+= i 1)
))
(return false)
))
(fn slice_eq (str slice_idx substr) (do
(let str_len (call len str))
(let substr_len (call len substr))
(let i slice_idx)
(loop (do
(if (or (>= (+ slice_idx i) str_len) (>= i substr_len))
(return false))
(if (!= (call at str (+ slice_idx i)) (call at substr i))
(return false))
(+= i 1)
))
(return true)
))
(let text (call read_text_file "program.phi"))
(let tokens (call tokenize text))
(call println "=== text ===")
(call println text)
(call println "=== tokens ===")
(call println tokens)
(call println (+ 1 2))