1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-11 13:55:55 +03:00

Fix Nim version for Nim 1.0.4

This commit is contained in:
Dennis Felsing 2020-01-13 16:01:59 +01:00 committed by def
parent 645d80f472
commit 41558f0177
15 changed files with 55 additions and 60 deletions

View File

@ -55,7 +55,7 @@
| [MATLAB](#matlab-gnu-octave-and-matlab) (GNU Octave & MATLAB) | [Joel Martin](https://github.com/kanaka) | | [MATLAB](#matlab-gnu-octave-and-matlab) (GNU Octave & MATLAB) | [Joel Martin](https://github.com/kanaka) |
| [miniMAL](#minimal) ([Repo](https://github.com/kanaka/miniMAL), [Demo](https://kanaka.github.io/miniMAL/)) | [Joel Martin](https://github.com/kanaka) | | [miniMAL](#minimal) ([Repo](https://github.com/kanaka/miniMAL), [Demo](https://kanaka.github.io/miniMAL/)) | [Joel Martin](https://github.com/kanaka) |
| [NASM](#nasm) | [Ben Dudson](https://github.com/bendudson) | | [NASM](#nasm) | [Ben Dudson](https://github.com/bendudson) |
| [Nim](#nim-0170) | [Dennis Felsing](https://github.com/def-) | | [Nim](#nim-104) | [Dennis Felsing](https://github.com/def-) |
| [Object Pascal](#object-pascal) | [Joel Martin](https://github.com/kanaka) | | [Object Pascal](#object-pascal) | [Joel Martin](https://github.com/kanaka) |
| [Objective C](#objective-c) | [Joel Martin](https://github.com/kanaka) | | [Objective C](#objective-c) | [Joel Martin](https://github.com/kanaka) |
| [OCaml](#ocaml-4010) | [Chris Houser](https://github.com/chouser) | | [OCaml](#ocaml-4010) | [Chris Houser](https://github.com/chouser) |
@ -675,9 +675,9 @@ make
./stepX_YYY ./stepX_YYY
``` ```
### Nim 0.17.0 ### Nim 1.0.4
The Nim implementation of mal has been tested with Nim 0.17.0. The Nim implementation of mal has been tested with Nim 1.0.4.
``` ```
cd nim cd nim

View File

@ -1,4 +1,4 @@
FROM ubuntu:vivid FROM ubuntu:bionic
MAINTAINER Joel Martin <github@martintribe.org> MAINTAINER Joel Martin <github@martintribe.org>
########################################################## ##########################################################
@ -26,10 +26,10 @@ RUN apt-get -y install g++
# Nim # Nim
RUN apt-get -y install xz-utils RUN apt-get -y install xz-utils
RUN cd /tmp && curl -O https://nim-lang.org/download/nim-0.17.2.tar.xz \ RUN cd /tmp && curl -O https://nim-lang.org/download/nim-1.0.4.tar.xz \
&& tar xvJf /tmp/nim-0.17.2.tar.xz && cd nim-0.17.2 \ && tar xvJf /tmp/nim-1.0.4.tar.xz && cd nim-1.0.4 \
&& make && sh install.sh /usr/local/bin \ && make && sh install.sh /usr/local/bin \
&& cp bin/nim /usr/local/bin/ \ && cp bin/nim /usr/local/bin/ \
&& rm -r /tmp/nim-0.17.2 && rm -r /tmp/nim-1.0.4
ENV HOME /mal ENV HOME /mal

View File

@ -1,4 +1,4 @@
import strutils, rdstdin, tables, algorithm, times, sequtils, types, printer, reader import strutils, rdstdin, tables, times, sequtils, types, printer, reader
type MalError* = object of Exception type MalError* = object of Exception
t*: MalType t*: MalType

View File

@ -17,8 +17,8 @@ proc pr_str*(m: MalType, pr = true): string =
of Symbol: result = m.str of Symbol: result = m.str
of String: result = m.str.str_handle(pr) of String: result = m.str.str_handle(pr)
of Number: result = $m.number of Number: result = $m.number
of List: result = "(" & m.list.mapIt(string, it.pr_str(pr)).join(" ") & ")" of List: result = "(" & m.list.mapIt(it.pr_str(pr)).join(" ") & ")"
of Vector: result = "[" & m.list.mapIt(string, it.pr_str(pr)).join(" ") & "]" of Vector: result = "[" & m.list.mapIt(it.pr_str(pr)).join(" ") & "]"
of HashMap: of HashMap:
result = "{" result = "{"
for key, val in m.hash_map.pairs: for key, val in m.hash_map.pairs:

View File

@ -1,4 +1,4 @@
import re, strutils, sequtils, types import options, re, strutils, types
let let
tokenRE = re"""[\s,]*(~@|[\[\]{}()'`~^@]|"(?:\\.|[^\\"])*"?|;.*|[^\s\[\]{}('"`,;)]*)""" tokenRE = re"""[\s,]*(~@|[\[\]{}()'`~^@]|"(?:\\.|[^\\"])*"?|;.*|[^\s\[\]{}('"`,;)]*)"""
@ -12,16 +12,13 @@ type
tokens: seq[string] tokens: seq[string]
position: int position: int
proc next(r: var Reader): string = proc next(r: var Reader): Option[string] =
if r.position >= r.tokens.len: if r.position < r.tokens.len:
result = nil result = r.tokens[r.position].some
else:
result = r.tokens[r.position]
inc r.position inc r.position
proc peek(r: Reader): string = proc peek(r: Reader): Option[string] =
if r.position >= r.tokens.len: nil if r.position < r.tokens.len: return r.tokens[r.position].some
else: r.tokens[r.position]
proc tokenize(str: string): seq[string] = proc tokenize(str: string): seq[string] =
result = @[] result = @[]
@ -31,7 +28,7 @@ proc tokenize(str: string): seq[string] =
var len = str.findBounds(tokenRE, matches, pos) var len = str.findBounds(tokenRE, matches, pos)
if len.first != -1 and len.last != -1 and len.last >= len.first: if len.first != -1 and len.last != -1 and len.last >= len.first:
pos = len.last + 1 pos = len.last + 1
if matches[0][0] != ';': if matches[0].len > 0 and matches[0][0] != ';':
result.add matches[0] result.add matches[0]
else: else:
inc pos inc pos
@ -41,11 +38,11 @@ proc read_form(r: var Reader): MalType
proc read_seq(r: var Reader, fr, to: string): seq[MalType] = proc read_seq(r: var Reader, fr, to: string): seq[MalType] =
result = @[] result = @[]
var t = r.next var t = r.next
if t != fr: raise newException(ValueError, "expected '" & fr & "'") if t.get("") != fr: raise newException(ValueError, "expected '" & fr & "'")
t = r.peek t = r.peek
while t != to: while t.get("") != to:
if t == nil: raise newException(ValueError, "expected '" & to & "', got EOF") if t.get("") == "": raise newException(ValueError, "expected '" & to & "', got EOF")
result.add r.read_form result.add r.read_form
t = r.peek t = r.peek
discard r.next discard r.next
@ -60,12 +57,12 @@ proc read_hash_map(r: var Reader): MalType =
result = hash_map r.read_seq("{", "}") result = hash_map r.read_seq("{", "}")
proc read_atom(r: var Reader): MalType = proc read_atom(r: var Reader): MalType =
let t = r.next let t = r.next.get("")
if t.match(intRE): number t.parseInt if t.match(intRE): number t.parseInt
elif t[0] == '"': elif t[0] == '"':
if not t.match(strRE): if not t.match(strRE):
raise newException(ValueError, "expected '\"', got EOF") raise newException(ValueError, "expected '\"', got EOF")
str t[1 .. <t.high].multiReplace(("\\\"", "\""), ("\\n", "\n"), ("\\\\", "\\")) str t[1 ..< t.high].multiReplace(("\\\"", "\""), ("\\n", "\n"), ("\\\\", "\\"))
elif t[0] == ':': keyword t[1 .. t.high] elif t[0] == ':': keyword t[1 .. t.high]
elif t == "nil": nilObj elif t == "nil": nilObj
elif t == "true": trueObj elif t == "true": trueObj
@ -73,10 +70,10 @@ proc read_atom(r: var Reader): MalType =
else: symbol t else: symbol t
proc read_form(r: var Reader): MalType = proc read_form(r: var Reader): MalType =
if r.peek[0] == ';': if r.peek.get("")[0] == ';':
discard r.next discard r.next
return nilObj return nilObj
case r.peek case r.peek.get("")
of "'": of "'":
discard r.next discard r.next
result = list(symbol "quote", r.read_form) result = list(symbol "quote", r.read_form)

View File

@ -11,9 +11,9 @@ proc eval_ast(ast: MalType, env: Table[string, MalType]): MalType =
raise newException(ValueError, "'" & ast.str & "' not found") raise newException(ValueError, "'" & ast.str & "' not found")
result = env[ast.str] result = env[ast.str]
of List: of List:
result = list ast.list.mapIt(MalType, it.eval(env)) result = list ast.list.mapIt(it.eval(env))
of Vector: of Vector:
result = vector ast.list.mapIt(MalType, it.eval(env)) result = vector ast.list.mapIt(it.eval(env))
of HashMap: of HashMap:
result = hash_map() result = hash_map()
for k, v in ast.hash_map.pairs: for k, v in ast.hash_map.pairs:

View File

@ -9,9 +9,9 @@ proc eval_ast(ast: MalType, env: var Env): MalType =
of Symbol: of Symbol:
result = env.get(ast.str) result = env.get(ast.str)
of List: of List:
result = list ast.list.mapIt(MalType, it.eval(env)) result = list ast.list.mapIt(it.eval(env))
of Vector: of Vector:
result = vector ast.list.mapIt(MalType, it.eval(env)) result = vector ast.list.mapIt(it.eval(env))
of HashMap: of HashMap:
result = hash_map() result = hash_map()
for k, v in ast.hash_map.pairs: for k, v in ast.hash_map.pairs:

View File

@ -9,9 +9,9 @@ proc eval_ast(ast: MalType, env: var Env): MalType =
of Symbol: of Symbol:
result = env.get(ast.str) result = env.get(ast.str)
of List: of List:
result = list ast.list.mapIt(MalType, it.eval(env)) result = list ast.list.mapIt(it.eval(env))
of Vector: of Vector:
result = vector ast.list.mapIt(MalType, it.eval(env)) result = vector ast.list.mapIt(it.eval(env))
of HashMap: of HashMap:
result = hash_map() result = hash_map()
for k, v in ast.hash_map.pairs: for k, v in ast.hash_map.pairs:

View File

@ -9,9 +9,9 @@ proc eval_ast(ast: MalType, env: var Env): MalType =
of Symbol: of Symbol:
result = env.get(ast.str) result = env.get(ast.str)
of List: of List:
result = list ast.list.mapIt(MalType, it.eval(env)) result = list ast.list.mapIt(it.eval(env))
of Vector: of Vector:
result = vector ast.list.mapIt(MalType, it.eval(env)) result = vector ast.list.mapIt(it.eval(env))
of HashMap: of HashMap:
result = hash_map() result = hash_map()
for k, v in ast.hash_map.pairs: for k, v in ast.hash_map.pairs:
@ -63,7 +63,7 @@ proc eval(ast: MalType, env: Env): MalType =
of "do": of "do":
let last = ast.list.high let last = ast.list.high
discard (list ast.list[1 .. <last]).eval_ast(env) discard (list ast.list[1 ..< last]).eval_ast(env)
ast = ast.list[last] ast = ast.list[last]
# Continue loop (TCO) # Continue loop (TCO)

View File

@ -9,9 +9,9 @@ proc eval_ast(ast: MalType, env: var Env): MalType =
of Symbol: of Symbol:
result = env.get(ast.str) result = env.get(ast.str)
of List: of List:
result = list ast.list.mapIt(MalType, it.eval(env)) result = list ast.list.mapIt(it.eval(env))
of Vector: of Vector:
result = vector ast.list.mapIt(MalType, it.eval(env)) result = vector ast.list.mapIt(it.eval(env))
of HashMap: of HashMap:
result = hash_map() result = hash_map()
for k, v in ast.hash_map.pairs: for k, v in ast.hash_map.pairs:
@ -63,7 +63,7 @@ proc eval(ast: MalType, env: Env): MalType =
of "do": of "do":
let last = ast.list.high let last = ast.list.high
discard (list ast.list[1 .. <last]).eval_ast(env) discard (list ast.list[1 ..< last]).eval_ast(env)
ast = ast.list[last] ast = ast.list[last]
# Continue loop (TCO) # Continue loop (TCO)

View File

@ -23,9 +23,9 @@ proc eval_ast(ast: MalType, env: var Env): MalType =
of Symbol: of Symbol:
result = env.get(ast.str) result = env.get(ast.str)
of List: of List:
result = list ast.list.mapIt(MalType, it.eval(env)) result = list ast.list.mapIt(it.eval(env))
of Vector: of Vector:
result = vector ast.list.mapIt(MalType, it.eval(env)) result = vector ast.list.mapIt(it.eval(env))
of HashMap: of HashMap:
result = hash_map() result = hash_map()
for k, v in ast.hash_map.pairs: for k, v in ast.hash_map.pairs:
@ -84,7 +84,7 @@ proc eval(ast: MalType, env: Env): MalType =
of "do": of "do":
let last = ast.list.high let last = ast.list.high
discard (list ast.list[1 .. <last]).eval_ast(env) discard (list ast.list[1 ..< last]).eval_ast(env)
ast = ast.list[last] ast = ast.list[last]
# Continue loop (TCO) # Continue loop (TCO)

View File

@ -33,9 +33,9 @@ proc eval_ast(ast: MalType, env: var Env): MalType =
of Symbol: of Symbol:
result = env.get(ast.str) result = env.get(ast.str)
of List: of List:
result = list ast.list.mapIt(MalType, it.eval(env)) result = list ast.list.mapIt(it.eval(env))
of Vector: of Vector:
result = vector ast.list.mapIt(MalType, it.eval(env)) result = vector ast.list.mapIt(it.eval(env))
of HashMap: of HashMap:
result = hash_map() result = hash_map()
for k, v in ast.hash_map.pairs: for k, v in ast.hash_map.pairs:
@ -105,7 +105,7 @@ proc eval(ast: MalType, env: Env): MalType =
of "do": of "do":
let last = ast.list.high let last = ast.list.high
discard (list ast.list[1 .. <last]).eval_ast(env) discard (list ast.list[1 ..< last]).eval_ast(env)
ast = ast.list[last] ast = ast.list[last]
# Continue loop (TCO) # Continue loop (TCO)

View File

@ -33,9 +33,9 @@ proc eval_ast(ast: MalType, env: var Env): MalType =
of Symbol: of Symbol:
result = env.get(ast.str) result = env.get(ast.str)
of List: of List:
result = list ast.list.mapIt(MalType, it.eval(env)) result = list ast.list.mapIt(it.eval(env))
of Vector: of Vector:
result = vector ast.list.mapIt(MalType, it.eval(env)) result = vector ast.list.mapIt(it.eval(env))
of HashMap: of HashMap:
result = hash_map() result = hash_map()
for k, v in ast.hash_map.pairs: for k, v in ast.hash_map.pairs:
@ -105,11 +105,10 @@ proc eval(ast: MalType, env: Env): MalType =
return ast.list[1].macroexpand(env) return ast.list[1].macroexpand(env)
of "try*": of "try*":
let let a1 = ast.list[1]
a1 = ast.list[1]
a2 = ast.list[2]
if ast.list.len <= 2: if ast.list.len <= 2:
return a1.eval(env) return a1.eval(env)
let a2 = ast.list[2]
if a2.list[0].str == "catch*": if a2.list[0].str == "catch*":
try: try:
return a1.eval(env) return a1.eval(env)
@ -126,7 +125,7 @@ proc eval(ast: MalType, env: Env): MalType =
of "do": of "do":
let last = ast.list.high let last = ast.list.high
discard (list ast.list[1 .. <last]).eval_ast(env) discard (list ast.list[1 ..< last]).eval_ast(env)
ast = ast.list[last] ast = ast.list[last]
# Continue loop (TCO) # Continue loop (TCO)

View File

@ -33,9 +33,9 @@ proc eval_ast(ast: MalType, env: var Env): MalType =
of Symbol: of Symbol:
result = env.get(ast.str) result = env.get(ast.str)
of List: of List:
result = list ast.list.mapIt(MalType, it.eval(env)) result = list ast.list.mapIt(it.eval(env))
of Vector: of Vector:
result = vector ast.list.mapIt(MalType, it.eval(env)) result = vector ast.list.mapIt(it.eval(env))
of HashMap: of HashMap:
result = hash_map() result = hash_map()
for k, v in ast.hash_map.pairs: for k, v in ast.hash_map.pairs:
@ -105,11 +105,10 @@ proc eval(ast: MalType, env: Env): MalType =
return ast.list[1].macroexpand(env) return ast.list[1].macroexpand(env)
of "try*": of "try*":
let let a1 = ast.list[1]
a1 = ast.list[1]
a2 = ast.list[2]
if ast.list.len <= 2: if ast.list.len <= 2:
return a1.eval(env) return a1.eval(env)
let a2 = ast.list[2]
if a2.list[0].str == "catch*": if a2.list[0].str == "catch*":
try: try:
return a1.eval(env) return a1.eval(env)
@ -126,7 +125,7 @@ proc eval(ast: MalType, env: Env): MalType =
of "do": of "do":
let last = ast.list.high let last = ast.list.high
discard (list ast.list[1 .. <last]).eval_ast(env) discard (list ast.list[1 ..< last]).eval_ast(env)
ast = ast.list[last] ast = ast.list[last]
# Continue loop (TCO) # Continue loop (TCO)

View File

@ -1,4 +1,4 @@
import tables, strutils import tables
type type
MalTypeKind* = enum Nil, True, False, Number, Symbol, String, MalTypeKind* = enum Nil, True, False, Number, Symbol, String,
@ -110,7 +110,7 @@ proc false_q*(xs: varargs[MalType]): MalType {.procvar.} =
boolObj xs[0].kind == False boolObj xs[0].kind == False
proc string_q*(xs: varargs[MalType]): MalType {.procvar.} = proc string_q*(xs: varargs[MalType]): MalType {.procvar.} =
boolObj(xs[0].kind == String and xs[0].str[0] != '\xff') boolObj(xs[0].kind == String and xs[0].str.len > 0 and xs[0].str[0] != '\xff')
proc symbol*(xs: varargs[MalType]): MalType {.procvar.} = proc symbol*(xs: varargs[MalType]): MalType {.procvar.} =
symbol(xs[0].str) symbol(xs[0].str)
@ -122,7 +122,7 @@ proc keyword*(xs: varargs[MalType]): MalType {.procvar.} =
keyword(xs[0].str) keyword(xs[0].str)
proc keyword_q*(xs: varargs[MalType]): MalType {.procvar.} = proc keyword_q*(xs: varargs[MalType]): MalType {.procvar.} =
boolObj(xs[0].kind == String and xs[0].str[0] == '\xff') boolObj(xs[0].kind == String and xs[0].str.len > 0 and xs[0].str[0] == '\xff')
proc number_q*(xs: varargs[MalType]): MalType {.procvar.} = proc number_q*(xs: varargs[MalType]): MalType {.procvar.} =
boolObj xs[0].kind == Number boolObj xs[0].kind == Number