all repos — min @ 11f8be2bb223ec7b610d0b491ed20c463d38d0d3

A small but practical concatenative programming language.

Refactoring: removed types.nim
h3rald h3rald@h3rald.com
Sun, 18 Sep 2016 15:01:09 +0200
commit

11f8be2bb223ec7b610d0b491ed20c463d38d0d3

parent

ebe3705e65a3d28564dc32b38b9d241651d8c445

A core/consts.nim

@@ -0,0 +1,14 @@

+import + os + +const version* = "1.0.0-dev" + +when defined(windows): + const HOME* = getenv("USERPROFILE") +when not defined(windows): + const HOME* = getenv("HOME") + +const MINIMRC* = HOME / ".minimrc" +const MINIMSYMBOLS* = HOME / ".minim_symbols" +const MINIMHISTORY* = HOME / ".minim_history" +
M core/interpreter.nimcore/interpreter.nim

@@ -1,7 +1,14 @@

-import streams, strutils, critbits, os +import + streams, + strutils, + critbits, + os import - types, parser + +type + MinRuntimeError* = ref object of SystemError + qVal*: seq[MinValue] proc raiseUndefined(msg: string) = raise MinUndefinedError(msg: msg)

@@ -14,13 +21,13 @@ raise MinInvalidError(msg: msg)

proc fullname*(scope: ref MinScope): string = result = scope.name - if scope.parent.isNotNil: + if not scope.parent.isNil: result = scope.parent.fullname & ":" & result proc getSymbol*(scope: ref MinScope, key: string): MinOperator = if scope.symbols.hasKey(key): return scope.symbols[key] - elif scope.parent.isNotNil: + elif not scope.parent.isNil: return scope.parent.getSymbol(key) else: raiseUndefined("Symbol '$1' not found." % key)

@@ -28,7 +35,7 @@

proc hasSymbol*(scope: ref MinScope, key: string): bool = if scope.symbols.hasKey(key): return true - elif scope.parent.isNotNil: + elif not scope.parent.isNil: return scope.parent.hasSymbol(key) else: return false

@@ -44,20 +51,20 @@

proc setSymbol*(scope: ref MinScope, key: string, value: MinOperator): bool {.discardable.}= result = false # check if a symbol already exists in current scope - if scope.isNotNil and scope.symbols.hasKey(key): + if not scope.isNil and scope.symbols.hasKey(key): if scope.symbols[key].sealed: raiseInvalid("Symbol '$1' is sealed." % key) scope.symbols[key] = value result = true else: # Go up the scope chain and attempt to find the symbol - if scope.parent.isNotNil: + if not scope.parent.isNil: result = scope.parent.setSymbol(key, value) proc getSigil*(scope: ref MinScope, key: string): MinOperator = if scope.sigils.hasKey(key): return scope.sigils[key] - elif scope.parent.isNotNil: + elif not scope.parent.isNil: return scope.parent.getSigil(key) else: raiseUndefined("Sigil '$1' not found." % key)

@@ -65,7 +72,7 @@

proc hasSigil*(scope: ref MinScope, key: string): bool = if scope.sigils.hasKey(key): return true - elif scope.parent.isNotNil: + elif not scope.parent.isNil: return scope.parent.hasSigil(key) else: return false
M core/parser.nimcore/parser.nim

@@ -1,6 +1,107 @@

# Adapted from: https://github.com/Araq/Nimrod/blob/v0.9.6/lib/pure/json.nim -import lexbase, strutils, streams, unicode, tables -import types +import + lexbase, + strutils, + streams, + unicode, + tables, + critbits + +type + MinTokenKind* = enum + tkError, + tkEof, + tkString, + tkInt, + tkFloat, + tkBracketLe, + tkBracketRi, + tkSymbol, + tkTrue, + tkFalse + MinKind* = enum + minInt, + minFloat, + minQuotation, + minString, + minSymbol, + minBool + MinEventKind* = enum ## enumeration of all events that may occur when parsing + eMinError, ## an error ocurred during parsing + eMinEof, ## end of file reached + eMinString, ## a string literal + eMinInt, ## an integer literal + eMinFloat, ## a float literal + eMinQuotationStart, ## start of an array: the ``(`` token + eMinQuotationEnd ## start of an array: the ``)`` token + MinParserError* = enum ## enumeration that lists all errors that can occur + errNone, ## no error + errInvalidToken, ## invalid token + errStringExpected, ## string expected + errBracketRiExpected, ## ``)`` expected + errQuoteExpected, ## ``"`` or ``'`` expected + errEOC_Expected, ## ``*/`` expected + errEofExpected, ## EOF expected + errExprExpected + MinParserState* = enum + stateEof, + stateStart, + stateQuotation, + stateExpectValue + MinParser* = object of BaseLexer + a*: string + token*: MinTokenKind + state*: seq[MinParserState] + kind*: MinEventKind + err*: MinParserError + filename*: string + MinValue* = object + line*: int + column*: int + filename*: string + case kind*: MinKind + of minInt: intVal*: BiggestInt + of minFloat: floatVal*: BiggestFloat + of minQuotation: + qVal*: seq[MinValue] + scope*: ref MinScope + of minString: strVal*: string + of minSymbol: symVal*: string + of minBool: boolVal*: bool + MinScope* = object + symbols*: CritBitTree[MinOperator] + sigils*: CritBitTree[MinOperator] + parent*: ref MinScope + name*: string + stack*: MinStack + MinOperatorProc* = proc (i: In) {.closure.} + MinOperatorKind* = enum + minProcOp + minValOp + MinOperator* = object + sealed*: bool + case kind*: MinOperatorKind + of minProcOp: + prc*: MinOperatorProc + of minValOp: + val*: MinValue + MinStack* = seq[MinValue] + In* = var MinInterpreter + MinInterpreter* = object + stack*: MinStack + pwd*: string + scope*: ref MinScope + parser*: MinParser + currSym*: MinValue + filename*: string + debugging*: bool + evaluating*: bool + unsafe*: bool + MinParsingError* = ref object of ValueError + MinUndefinedError* = ref object of ValueError + MinEmptyStackError* = ref object of ValueError + MinInvalidError* = ref object of ValueError + const errorMessages: array [MinParserError, string] = [
D core/types.nim

@@ -1,116 +0,0 @@

-import lexbase, critbits, os - -type - MinTokenKind* = enum - tkError, - tkEof, - tkString, - tkInt, - tkFloat, - tkBracketLe, - tkBracketRi, - tkSymbol, - tkTrue, - tkFalse - MinKind* = enum - minInt, - minFloat, - minQuotation, - minString, - minSymbol, - minBool - MinScope* = object - symbols*: CritBitTree[MinOperator] - sigils*: CritBitTree[MinOperator] - parent*: ref MinScope - name*: string - stack*: MinStack - MinValue* = object - line*: int - column*: int - filename*: string - case kind*: MinKind - of minInt: intVal*: BiggestInt - of minFloat: floatVal*: BiggestFloat - of minQuotation: - qVal*: seq[MinValue] - scope*: ref MinScope - of minString: strVal*: string - of minSymbol: symVal*: string - of minBool: boolVal*: bool - MinEventKind* = enum ## enumeration of all events that may occur when parsing - eMinError, ## an error ocurred during parsing - eMinEof, ## end of file reached - eMinString, ## a string literal - eMinInt, ## an integer literal - eMinFloat, ## a float literal - eMinQuotationStart, ## start of an array: the ``(`` token - eMinQuotationEnd ## start of an array: the ``)`` token - MinParserError* = enum ## enumeration that lists all errors that can occur - errNone, ## no error - errInvalidToken, ## invalid token - errStringExpected, ## string expected - errBracketRiExpected, ## ``)`` expected - errQuoteExpected, ## ``"`` or ``'`` expected - errEOC_Expected, ## ``*/`` expected - errEofExpected, ## EOF expected - errExprExpected - MinParserState* = enum - stateEof, - stateStart, - stateQuotation, - stateExpectValue - MinParser* = object of BaseLexer - a*: string - token*: MinTokenKind - state*: seq[MinParserState] - kind*: MinEventKind - err*: MinParserError - filename*: string - MinStack* = seq[MinValue] - MinInterpreter* = object - stack*: MinStack - pwd*: string - scope*: ref MinScope - parser*: MinParser - currSym*: MinValue - filename*: string - debugging*: bool - evaluating*: bool - unsafe*: bool - In* = var MinInterpreter - Val* = var MinValue - MinOperatorProc* = proc (i: In) {.closure.} - MinOperatorKind* = enum - minProcOp - minValOp - MinOperator* = object - sealed*: bool - case kind*: MinOperatorKind - of minProcOp: - prc*: MinOperatorProc - of minValOp: - val*: MinValue - MinSigil* = proc (i: In, sym: string) - MinParsingError* = ref object of ValueError - MinUndefinedError* = ref object of ValueError - MinInvalidError* = ref object of ValueError - MinEmptyStackError* = ref object of ValueError - MinOutOfBoundsError* = ref object of ValueError - MinRuntimeError* = ref object of SystemError - qVal*: seq[MinValue] - -proc isNotNil*[T](obj: T): bool = - return not obj.isNil - -const version* = "1.0.0-dev" - -when defined(windows): - const HOME* = getenv("USERPROFILE") -when not defined(windows): - const HOME* = getenv("HOME") - -const MINIMRC* = HOME / ".minimrc" -const MINIMSYMBOLS* = HOME / ".minim_symbols" -const MINIMHISTORY* = HOME / ".minim_history" -
M core/utils.nimcore/utils.nim

@@ -7,9 +7,12 @@ json,

os, regex import - types, parser, interpreter + +type + MinOutOfBoundsError* = ref object of ValueError + proc isSymbol*(s: MinValue): bool = return s.kind == minSymbol
M lib/min_crypto.nimlib/min_crypto.nim

@@ -4,7 +4,6 @@ base64,

strutils, times import - ../core/types, ../core/parser, ../core/interpreter, ../core/utils,
M lib/min_fs.nimlib/min_fs.nim

@@ -3,7 +3,6 @@ strutils,

os, times import - ../core/types, ../core/parser, ../core/interpreter, ../core/utils
M lib/min_io.nimlib/min_io.nim

@@ -1,7 +1,8 @@

-import os, strutils +import + os, + strutils import ../core/linedit, - ../core/types, ../core/parser, ../core/interpreter, ../core/utils
M lib/min_lang.nimlib/min_lang.nim

@@ -4,7 +4,7 @@ strutils,

os, json import - ../core/types, + ../core/consts, ../core/parser, ../core/interpreter, ../core/utils,

@@ -20,7 +20,7 @@

.symbol("symbols") do (i: In): var q = newSeq[MinValue](0) var scope = i.scope - while scope.isNotNil: + while not scope.isNil: for s in scope.symbols.keys: q.add s.newVal scope = scope.parent

@@ -29,7 +29,7 @@

.symbol("sigils") do (i: In): var q = newSeq[MinValue](0) var scope = i.scope - while scope.isNotNil: + while not scope.isNil: for s in scope.sigils.keys: q.add s.newVal scope = scope.parent

@@ -121,10 +121,9 @@ var mdl, rawName: MinValue

var name: string i.reqStringLike rawName name = rawName.getString - #i.scope.getSymbol(name).prc(i) i.execOp(i.scope.getSymbol(name)) i.reqQuotation mdl - if mdl.scope.isNotNil: + if not mdl.scope.isNil: for sym, val in mdl.scope.symbols.pairs: i.debug "[import] $1:$2" % [i.scope.name, sym] i.scope.symbols[sym] = val
M lib/min_logic.nimlib/min_logic.nim

@@ -1,6 +1,6 @@

-import tables import - ../core/types, + tables +import ../core/parser, ../core/interpreter, ../core/utils
M lib/min_num.nimlib/min_num.nim

@@ -2,7 +2,6 @@ import

tables, random import - ../core/types, ../core/parser, ../core/interpreter, ../core/utils
M lib/min_str.nimlib/min_str.nim

@@ -1,6 +1,8 @@

-import tables, strutils, sequtils import - ../core/types, + tables, + strutils, + sequtils +import ../core/parser, ../core/interpreter, ../core/utils,
M lib/min_sys.nimlib/min_sys.nim

@@ -1,6 +1,9 @@

-import tables, os, osproc, strutils +import + tables, + os, + osproc, + strutils import - ../core/types, ../core/parser, ../core/interpreter, ../core/utils
M lib/min_time.nimlib/min_time.nim

@@ -1,6 +1,7 @@

-import times, tables +import + times, + tables import - ../core/types, ../core/parser, ../core/interpreter, ../core/utils
M minim.nimminim.nim

@@ -1,7 +1,7 @@

import streams, critbits, parseopt2, strutils, os, json, sequtils import core/linedit, - core/types, + core/consts, core/parser, core/interpreter, core/utils