Skip to content

Commit

Permalink
Added some cool functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dom96 committed Jun 12, 2010
1 parent 627cda9 commit 0f498cc
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
37 changes: 37 additions & 0 deletions src/core.nim
Expand Up @@ -174,6 +174,18 @@ proc command*(cmnd: string, dataStack: var TStack, vars, gvars: var PType) =
if first.kind == ntInt and second.kind == ntInt:
dataStack.push(newBool(second.iValue < first.iValue))

of "and":
var first = dataStack.pop()
var second = dataStack.pop()
if first.kind == ntBool and second.kind == ntBool:
dataStack.push(newBool(second.bValue and first.bValue))

of "or":
var first = dataStack.pop()
var second = dataStack.pop()
if first.kind == ntBool and second.kind == ntBool:
dataStack.push(newBool(second.bValue or first.bValue))

of "=":
var first = dataStack.pop()
var second = dataStack.pop()
Expand Down Expand Up @@ -280,13 +292,38 @@ proc command*(cmnd: string, dataStack: var TStack, vars, gvars: var PType) =
else:
raise invalidTypeErr($first.kind & " and " & $second.kind, "float and float", "pow")

of "round":
var first = dataStack.pop()
if first.kind == ntFloat:
dataStack.push(newInt(round(first.fValue)))
else:
raise invalidTypeErr($first.kind, "float", "round")

# Type conversion
of "type":
var first = dataStack.pop()
dataStack.push(newString($first.kind))

of "int>float":
var first = dataStack.pop()
if first.kind == ntInt:
dataStack.push(newFloat(float(first.iValue)))
else:
raise invalidTypeErr($first.kind, "int", "int>float")

of "float>int":
var first = dataStack.pop()
if first.kind == ntFloat:
dataStack.push(newInt(int(first.fValue)))
else:
raise invalidTypeErr($first.kind, "float", "float>int")

of "string>int":
var first = dataStack.pop()
if first.kind == ntString:
dataStack.push(newInt(first.value.parseInt()))
else:
raise invalidTypeErr($first.kind, "string", "string>int")

# Exception handling
of "try":
Expand Down
15 changes: 15 additions & 0 deletions src/interpreter.nim
Expand Up @@ -109,6 +109,21 @@ proc toString*(item: PType, stack = False): string =
else:
raise newException(ERuntimeError, errorLine() & "Error: Unexpected AstNode in `$`, " & $item.node.kind)

proc `$`*(kind: TTypes): string =
case kind
of ntInt: return "int"
of ntFloat: return "float"
of ntString: return "string"
of ntBool: return "bool"
of ntList: return "list"
of ntQuot: return "quot"
of ntDict: return "dict"
of ntNil: return "nil"
of ntCmnd: return "__cmnd__"
of ntVar: return "var"
of ntFunc: return "__func__"
of ntASTNode: return "__ASTNode__"

proc isEqual*(first, second: PType): bool =
if first.kind == second.kind:
case first.kind
Expand Down
3 changes: 3 additions & 0 deletions src/system.nael
Expand Up @@ -23,3 +23,6 @@ each [list, quo]
);

join [list, seperator] (i let, i 0 =, s let, s "" =, (i get, list get len 1 - <) (s get (i get 0 == !) (seperator get +) () if, list get, i get nth, + s swap =, i i get inc =) while, s get);

<= [first, second] (first get second get == first get second get < or);
>= [x,y] (y get x get <=);
5 changes: 3 additions & 2 deletions todo.markdown
Expand Up @@ -5,7 +5,7 @@ nael todo list
* tuples
* dictionaries
* Loading of C Libraries
* Functions for converting types
* Functions for converting types - **Partly Done**
* `string>int`
* `int>string`
* `float>string`
Expand All @@ -14,4 +14,5 @@ nael todo list
* **FIXED**
* Make every error, report it's origin(function).
* `[0, 0] Error: Wrong types... @ +` - **Partly done**
* Make global variables work, the `def` keyword.
* Make global variables work, the `def` keyword.
* Make raising exceptions work, and change any functions that need exception raising, to use raise.

0 comments on commit 0f498cc

Please sign in to comment.