1
1
mirror of https://github.com/kanaka/mal.git synced 2024-08-16 09:10:48 +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) |
| [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) |
| [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) |
| [Objective C](#objective-c) | [Joel Martin](https://github.com/kanaka) |
| [OCaml](#ocaml-4010) | [Chris Houser](https://github.com/chouser) |
@ -675,9 +675,9 @@ make
./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

View File

@ -1,4 +1,4 @@
FROM ubuntu:vivid
FROM ubuntu:bionic
MAINTAINER Joel Martin <github@martintribe.org>
##########################################################
@ -26,10 +26,10 @@ RUN apt-get -y install g++
# Nim
RUN apt-get -y install xz-utils
RUN cd /tmp && curl -O https://nim-lang.org/download/nim-0.17.2.tar.xz \
&& tar xvJf /tmp/nim-0.17.2.tar.xz && cd nim-0.17.2 \
RUN cd /tmp && curl -O https://nim-lang.org/download/nim-1.0.4.tar.xz \
&& tar xvJf /tmp/nim-1.0.4.tar.xz && cd nim-1.0.4 \
&& make && sh install.sh /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

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
t*: MalType

View File

@ -17,8 +17,8 @@ proc pr_str*(m: MalType, pr = true): string =
of Symbol: result = m.str
of String: result = m.str.str_handle(pr)
of Number: result = $m.number
of List: result = "(" & m.list.mapIt(string, it.pr_str(pr)).join(" ") & ")"
of Vector: 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(it.pr_str(pr)).join(" ") & "]"
of HashMap:
result = "{"
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
tokenRE = re"""[\s,]*(~@|[\[\]{}()'`~^@]|"(?:\\.|[^\\"])*"?|;.*|[^\s\[\]{}('"`,;)]*)"""
@ -12,16 +12,13 @@ type
tokens: seq[string]
position: int
proc next(r: var Reader): string =
if r.position >= r.tokens.len:
result = nil
else:
result = r.tokens[r.position]
proc next(r: var Reader): Option[string] =
if r.position < r.tokens.len:
result = r.tokens[r.position].some
inc r.position
proc peek(r: Reader): string =
if r.position >= r.tokens.len: nil
else: r.tokens[r.position]
proc peek(r: Reader): Option[string] =
if r.position < r.tokens.len: return r.tokens[r.position].some
proc tokenize(str: string): seq[string] =
result = @[]
@ -31,7 +28,7 @@ proc tokenize(str: string): seq[string] =
var len = str.findBounds(tokenRE, matches, pos)
if len.first != -1 and len.last != -1 and len.last >= len.first:
pos = len.last + 1
if matches[0][0] != ';':
if matches[0].len > 0 and matches[0][0] != ';':
result.add matches[0]
else:
inc pos
@ -41,11 +38,11 @@ proc read_form(r: var Reader): MalType
proc read_seq(r: var Reader, fr, to: string): seq[MalType] =
result = @[]
var t = r.next
if t != fr: raise newException(ValueError, "expected '" & fr & "'")
if t.get("") != fr: raise newException(ValueError, "expected '" & fr & "'")
t = r.peek
while t != to:
if t == nil: raise newException(ValueError, "expected '" & to & "', got EOF")
while t.get("") != to:
if t.get("") == "": raise newException(ValueError, "expected '" & to & "', got EOF")
result.add r.read_form
t = r.peek
discard r.next
@ -60,12 +57,12 @@ proc read_hash_map(r: var Reader): MalType =
result = hash_map r.read_seq("{", "}")
proc read_atom(r: var Reader): MalType =
let t = r.next
let t = r.next.get("")
if t.match(intRE): number t.parseInt
elif t[0] == '"':
if not t.match(strRE):
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 == "nil": nilObj
elif t == "true": trueObj
@ -73,10 +70,10 @@ proc read_atom(r: var Reader): MalType =
else: symbol t
proc read_form(r: var Reader): MalType =
if r.peek[0] == ';':
if r.peek.get("")[0] == ';':
discard r.next
return nilObj
case r.peek
case r.peek.get("")
of "'":
discard r.next
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")
result = env[ast.str]
of List:
result = list ast.list.mapIt(MalType, it.eval(env))
result = list ast.list.mapIt(it.eval(env))
of Vector:
result = vector ast.list.mapIt(MalType, it.eval(env))
result = vector ast.list.mapIt(it.eval(env))
of HashMap:
result = hash_map()
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:
result = env.get(ast.str)
of List:
result = list ast.list.mapIt(MalType, it.eval(env))
result = list ast.list.mapIt(it.eval(env))
of Vector:
result = vector ast.list.mapIt(MalType, it.eval(env))
result = vector ast.list.mapIt(it.eval(env))
of HashMap:
result = hash_map()
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:
result = env.get(ast.str)
of List:
result = list ast.list.mapIt(MalType, it.eval(env))
result = list ast.list.mapIt(it.eval(env))
of Vector:
result = vector ast.list.mapIt(MalType, it.eval(env))
result = vector ast.list.mapIt(it.eval(env))
of HashMap:
result = hash_map()
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:
result = env.get(ast.str)
of List:
result = list ast.list.mapIt(MalType, it.eval(env))
result = list ast.list.mapIt(it.eval(env))
of Vector:
result = vector ast.list.mapIt(MalType, it.eval(env))
result = vector ast.list.mapIt(it.eval(env))
of HashMap:
result = hash_map()
for k, v in ast.hash_map.pairs:
@ -63,7 +63,7 @@ proc eval(ast: MalType, env: Env): MalType =
of "do":
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]
# Continue loop (TCO)

View File

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

View File

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

View File

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

View File

@ -33,9 +33,9 @@ proc eval_ast(ast: MalType, env: var Env): MalType =
of Symbol:
result = env.get(ast.str)
of List:
result = list ast.list.mapIt(MalType, it.eval(env))
result = list ast.list.mapIt(it.eval(env))
of Vector:
result = vector ast.list.mapIt(MalType, it.eval(env))
result = vector ast.list.mapIt(it.eval(env))
of HashMap:
result = hash_map()
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)
of "try*":
let
a1 = ast.list[1]
a2 = ast.list[2]
let a1 = ast.list[1]
if ast.list.len <= 2:
return a1.eval(env)
let a2 = ast.list[2]
if a2.list[0].str == "catch*":
try:
return a1.eval(env)
@ -126,7 +125,7 @@ proc eval(ast: MalType, env: Env): MalType =
of "do":
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]
# Continue loop (TCO)

View File

@ -33,9 +33,9 @@ proc eval_ast(ast: MalType, env: var Env): MalType =
of Symbol:
result = env.get(ast.str)
of List:
result = list ast.list.mapIt(MalType, it.eval(env))
result = list ast.list.mapIt(it.eval(env))
of Vector:
result = vector ast.list.mapIt(MalType, it.eval(env))
result = vector ast.list.mapIt(it.eval(env))
of HashMap:
result = hash_map()
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)
of "try*":
let
a1 = ast.list[1]
a2 = ast.list[2]
let a1 = ast.list[1]
if ast.list.len <= 2:
return a1.eval(env)
let a2 = ast.list[2]
if a2.list[0].str == "catch*":
try:
return a1.eval(env)
@ -126,7 +125,7 @@ proc eval(ast: MalType, env: Env): MalType =
of "do":
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]
# Continue loop (TCO)

View File

@ -1,4 +1,4 @@
import tables, strutils
import tables
type
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
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.} =
symbol(xs[0].str)
@ -122,7 +122,7 @@ proc keyword*(xs: varargs[MalType]): MalType {.procvar.} =
keyword(xs[0].str)
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.} =
boolObj xs[0].kind == Number