all repos — min @ b6e64f361b0774eee95720df2ab98bd14125d051

A small but practical concatenative programming language.

Replaces tables with critbits; moved types to dedicated file.
h3rald h3rald@h3rald.com
Fri, 20 May 2016 21:36:42 +0200
commit

b6e64f361b0774eee95720df2ab98bd14125d051

parent

92684f1dd61207992926808fb7b1c445aa6337f3

M core/interpreter.nimcore/interpreter.nim

@@ -1,27 +1,5 @@

-import streams, strutils, tables -import parser, ../vendor/linenoise - -type - MinInterpreter* = object - stack*: MinStack - parser*: MinParser - currSym: MinValue - filename*: string - debugging*: bool - evaluating*: bool - MinOperator* = proc (i: var MinInterpreter) - MinSigil* = proc (i: var MinInterpreter, sym: string) - MinError* = enum - errSystem, - errParser, - errGeneric, - errEmptyStack, - errNoQuotation, - errUndefined, - errIncorrect, - errTwoNumbersRequired, - errDivisionByZero - +import streams, strutils, critbits +import types, parser, ../vendor/linenoise const ERRORS: array [MinError, string] = [ "A system error occurred",

@@ -35,8 +13,8 @@ "Two numbers are required on the stack",

"Division by zero" ] -var SYMBOLS* = initTable[string, MinOperator]() -var SIGILS* = initTable[string, MinOperator]() +var SYMBOLS*: CritBitTree[MinOperator] +var SIGILS*: CritBitTree[MinOperator] proc newMinInterpreter*(debugging = false): MinInterpreter = var s:MinStack = newSeq[MinValue](0)
M core/parser.nimcore/parser.nim

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

# Adapted from: https://github.com/Araq/Nimrod/blob/v0.9.6/lib/pure/min.nim import lexbase, strutils, streams, unicode, tables - -type - MinTokenKind* = enum - tkError, - tkEof, - tkString, - tkInt, - tkFloat, - tkBracketLe, - tkBracketRi, - tkSymbol, - tkTrue, - tkFalse - MinKind* = enum - minInt, - minFloat, - minQuotation, - minString, - minSymbol, - minBool - MinValue* = object - line*: int - column*: int - case kind*: MinKind - of minInt: intVal*: int - of minFloat: floatVal*: float - of minQuotation: qVal*: seq[MinValue] - 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] - EMinParsingError* = ref object of ValueError - EMinUndefinedError* = ref object of ValueError +import types const errorMessages: array [MinParserError, string] = [
A core/types.nim

@@ -0,0 +1,90 @@

+import lexbase, critbits + +type + MinTokenKind* = enum + tkError, + tkEof, + tkString, + tkInt, + tkFloat, + tkBracketLe, + tkBracketRi, + tkSymbol, + tkTrue, + tkFalse + MinKind* = enum + minInt, + minFloat, + minQuotation, + minString, + minSymbol, + minBool + MinScope = object + locals: CritBitTree[MinValue] + symbols: CritBitTree[MinValue] + sigils: CritBitTree[MinValue] + parent: ref MinScope + stack: MinStack + MinValue* = object + line*: int + column*: int + case kind*: MinKind + of minInt: intVal*: int + of minFloat: floatVal*: float + of minQuotation: + qVal*: seq[MinValue] + scope: 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] + EMinParsingError* = ref object of ValueError + EMinUndefinedError* = ref object of ValueError + MinInterpreter* = object + stack*: MinStack + parser*: MinParser + currSym*: MinValue + filename*: string + debugging*: bool + evaluating*: bool + MinOperator* = proc (i: var MinInterpreter) + MinSigil* = proc (i: var MinInterpreter, sym: string) + MinError* = enum + errSystem, + errParser, + errGeneric, + errEmptyStack, + errNoQuotation, + errUndefined, + errIncorrect, + errTwoNumbersRequired, + errDivisionByZero
M core/utils.nimcore/utils.nim

@@ -1,5 +1,5 @@

-import tables, strutils -import parser, interpreter +import tables, strutils, macros, critbits +import types, parser, interpreter template minsym*(name: string, i: expr, body: stmt): stmt {.immediate.} =
M lib/io.nimlib/io.nim

@@ -1,5 +1,9 @@

import tables, os, strutils -import ../core/parser, ../core/interpreter, ../core/utils +import + ../core/types, + ../core/parser, + ../core/interpreter, + ../core/utils # I/O
M lib/lang.nimlib/lang.nim

@@ -1,5 +1,9 @@

-import tables, strutils -import ../core/parser, ../core/interpreter, ../core/utils +import critbits, strutils +import + ../core/types, + ../core/parser, + ../core/interpreter, + ../core/utils minsym "exit", i: quit(0)

@@ -48,7 +52,7 @@ minsym "unbind", i:

var q1 = i.pop if q1.qVal.len == 1 and q1.qVal[0].kind == minSymbol: var symbol = q1.qVal[0].symVal - SYMBOLS.del symbol + SYMBOLS.excl symbol else: i.error errIncorrect, "The top quotation must contain only one symbol value"
M lib/logic.nimlib/logic.nim

@@ -1,5 +1,9 @@

import tables -import ../core/parser, ../core/interpreter, ../core/utils +import + ../core/types, + ../core/parser, + ../core/interpreter, + ../core/utils # Comparison operators
M lib/numbers.nimlib/numbers.nim

@@ -1,5 +1,9 @@

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

@@ -1,5 +1,9 @@

import tables -import ../core/parser, ../core/interpreter, ../core/utils +import + ../core/types, + ../core/parser, + ../core/interpreter, + ../core/utils # Operations on quotations
M lib/stack.nimlib/stack.nim

@@ -1,5 +1,9 @@

import tables -import ../core/interpreter, ../core/utils +import + ../core/types, + ../core/parser, + ../core/interpreter, + ../core/utils # Common stack operations
M lib/strings.nimlib/strings.nim

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

import tables, strutils -import ../core/parser, ../core/interpreter, ../core/utils import ../vendor/slre +import + ../core/types, + ../core/parser, + ../core/interpreter, + ../core/utils minsym "split", i: let sep = i.pop
M lib/sys.nimlib/sys.nim

@@ -1,5 +1,9 @@

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

@@ -1,5 +1,9 @@

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

@@ -1,4 +1,4 @@

-import streams, tables, parseopt2, strutils +import streams, critbits, parseopt2, strutils import core/parser, core/interpreter,