Skip to content

Commit

Permalink
Update 0.9.0 patch.
Browse files Browse the repository at this point in the history
Range returns a list now.
Fixed bad list creation on many list/ methods.
Optimized embed runtime: Now a block instead of a closure.
Added access expressions for fields. Updated docs.
Imports can be anonymous now.
Better error reporting.
  • Loading branch information
sigmasoldi3r committed Jul 21, 2020
1 parent 5011a82 commit e6905f1
Show file tree
Hide file tree
Showing 10 changed files with 225 additions and 118 deletions.
42 changes: 21 additions & 21 deletions charon-runtime.lua
Original file line number Diff line number Diff line change
Expand Up @@ -159,17 +159,17 @@ function charon.list_has(tbl, element, finder)
end

function charon.list_get(tbl, key)
assert(getmetatable(tbl) == List, "list/get only accepts vectors.");
assert(getmetatable(tbl) == List, "list/get only accepts lists.");
assert(type(key) == 'number', "list/get key can only be numeric.");
local field = tbl[key];
if field == nil then return charon.Unit; end
return field;
end

function charon.list_merge(left, right)
assert(getmetatable(left) == List, "list/merge only accepts vectors.");
assert(getmetatable(right) == List, "list/merge only accepts vectors.");
local vec = charon.vector{};
assert(getmetatable(left) == List, "list/merge only accepts lists.");
assert(getmetatable(right) == List, "list/merge only accepts lists.");
local vec = charon.list{};
for _, v in pairs(left) do
vec[#vec + 1] = v;
end
Expand All @@ -180,12 +180,12 @@ function charon.list_merge(left, right)
end

function charon.list_len(left)
assert(getmetatable(left) == List, "list/add only accepts vectors.");
assert(getmetatable(left) == List, "list/add only accepts lists.");
return #left;
end

function charon.list_reduce_indexed(vec, fn, value)
assert(getmetatable(vec) == List, "list/reduce-indexed only accepts vectors.");
assert(getmetatable(vec) == List, "list/reduce-indexed only accepts lists.");
local start = 1;
if value == nil then
start = 2;
Expand All @@ -198,7 +198,7 @@ function charon.list_reduce_indexed(vec, fn, value)
end

function charon.list_reduce(vec, fn, value)
assert(getmetatable(vec) == List, "list/reduce only accepts vectors.");
assert(getmetatable(vec) == List, "list/reduce only accepts lists.");
local start = 1;
if value == nil then
start = 2;
Expand All @@ -211,8 +211,8 @@ function charon.list_reduce(vec, fn, value)
end

function charon.list_append(left, ...)
assert(getmetatable(left) == List, "list/append only accepts vectors.");
local vec = charon.vector{};
assert(getmetatable(left) == List, "list/append only accepts lists.");
local vec = charon.list{};
for _, v in pairs(left) do
vec[#vec + 1] = v;
end
Expand All @@ -223,8 +223,8 @@ function charon.list_append(left, ...)
end

function charon.list_prepend(left, ...)
assert(getmetatable(left) == List, "list/prepend only accepts vectors.");
local vec = charon.vector{};
assert(getmetatable(left) == List, "list/prepend only accepts lists.");
local vec = charon.list{};
for _, v in pairs(left) do
vec[#vec + 1] = v;
end
Expand All @@ -235,9 +235,9 @@ function charon.list_prepend(left, ...)
end

function charon.list_drop(left, n)
assert(getmetatable(left) == List, "list/drop only accepts vectors.");
assert(getmetatable(left) == List, "list/drop only accepts lists.");
assert(type(n) == 'number', "list/drop second argument must be a number.");
local vec = charon.vector{};
local vec = charon.list{};
local min = math.min(#left, n);
for i=1, min do
vec[i] = left[i];
Expand All @@ -246,9 +246,9 @@ function charon.list_drop(left, n)
end

function charon.list_drop_left(left, n)
assert(getmetatable(left) == List, "list/drop-left only accepts vectors.");
assert(getmetatable(left) == List, "list/drop-left only accepts lists.");
assert(type(n) == 'number', "list/drop-left second argument must be a number.");
local vec = charon.vector{};
local vec = charon.list{};
local min = math.min(#left, n);
for i=min, #left do
vec[i] = left[i];
Expand All @@ -257,17 +257,17 @@ function charon.list_drop_left(left, n)
end

function charon.list_map(tbl, mapper)
assert(getmetatable(tbl) == List, "list/map only accepts vectors.");
local vec = charon.vector{};
assert(getmetatable(tbl) == List, "list/map only accepts lists.");
local vec = charon.list{};
for k, v in pairs(tbl) do
vec[#vec + 1] = mapper(v, k);
end
return vec;
end

function charon.list_filter(tbl, filter)
assert(getmetatable(tbl) == List, "list/map only accepts vectors.");
local vec = charon.vector{};
assert(getmetatable(tbl) == List, "list/map only accepts lists.");
local vec = charon.list{};
for k, v in pairs(tbl) do
if filter(v, k) then
vec[#vec + 1] = v;
Expand All @@ -277,7 +277,7 @@ function charon.list_filter(tbl, filter)
end

function charon.list_each(tbl, consumer)
assert(getmetatable(tbl) == List, "list/each only accepts vectors.");
assert(getmetatable(tbl) == List, "list/each only accepts lists.");
for k, v in pairs(tbl) do
consumer(v, k);
end
Expand Down Expand Up @@ -471,7 +471,7 @@ function charon.range(from, to, inc)
assert(inc == nil or type(inc) == 'number'
, 'Range\'s third argument can only be a number or not provided. Saw ' .. tostring(inc) .. ' instead.')
if inc == nil then inc = 1; end
local p = {};
local p = charon.list{};
local j = 1;
if from > to then
for i=from, to, -inc do
Expand Down
41 changes: 41 additions & 0 deletions docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,47 @@ A list of objects, functions and operators.
- `file/write`
- `file/read`

## Module system

Charon lang modules are exported tables. To maximize the interop between pure
Lua modules and Charon modules, Charon exports a table with all public objects,
either functions or variables, with their names untouched.

The same goes when importing, the `import` macro will bind to a private local
variable the imported module. The macro also has the option to deconstruct the
object and assign it's fields.

```clj
; Imports the whole table
(import module :from "module")
(println! (+ (object/get module "a") module::b module::c))

; Or just the needed fields
(import [a b c] :from "module")
(println! (+ a b c))
```

At the moment there is no metadata field exported from modules, but it will
have in a future.

Modules will tell if the exported object has a known type, if it is callable or
not and the purity in case of callable objects, even attached metadata and
docstring.

```clj
(def a-func [a b]
"It sums"
(+ a b))
;; Other module
(import [a-func] :from "a-module")
(def-value :no-export a-func-meta (object/get-meta a-func))
(println! "a-func:" a-func-meta::docstring)
(println! "a-func purity:" a-func-meta::pure?)
```

That's a practical example of how to make a runtime read of the module's
metadata, **not implemented yet!**.

## Library objects and collections

### string
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "charon",
"version": "0.8.1",
"version": "0.9.0",
"preview": true,
"description": "Charon language compiler",
"main": "dist/index.js",
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ of "assigning" a variable as other languages do.

## Standard library

Module system is described in [docs/Module System](docs.md#module-system).

Charon standard library is small, it relies on Lua standard library for most
things.

Expand Down
19 changes: 10 additions & 9 deletions resources/charon.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
*/

Program
= _ program:(r:Invoke _ { return r })*
= _ program:(r:Invoke _ { return r })* (';' [^\r\n]*)?
{
return { type: 'Program', program }
}

Term
= Vector
= List
/ Table
/ Invoke
/ Literal
/ AccessExpression
/ NAME
/ WILDCARD

Expand Down Expand Up @@ -52,23 +53,23 @@ AccessSegment
}
}

Vector
= LSQUARE_BRACE _ list:(e:Term _ { return e })* RSQUARE_BRACE
List
= LSQUARE_BRACE _ values:(e:Term _ { return e })* RSQUARE_BRACE
{
return {
_location: location(),
type: 'Vector',
list
type: 'List',
values
}
}

Table
= LBRACE _ list:(e:Term _ { return e })* RBRACE
= LBRACE _ values:(e:Term _ { return e })* RBRACE
{
return {
_location: location(),
type: 'Table',
list
values
}
}

Expand All @@ -95,7 +96,7 @@ NUMBER
{ return { _location: location(), type: 'Token', value, name: 'NUMBER' } }

STRING
= value:$('"' [^"]* '"')
= value:$('"' ('\\"' / [^"])* '"')
{ return { _location: location(), type: 'Token', value, name: 'STRING' } }

SYMBOL
Expand Down
Loading

0 comments on commit e6905f1

Please sign in to comment.