Skip to content

Commit

Permalink
Merge pull request #448 from bvssvni/namespace
Browse files Browse the repository at this point in the history
Optional namespaces with shared aliases
  • Loading branch information
bvssvni authored Mar 5, 2017
2 parents 3b24982 + 2ddd943 commit 662d943
Show file tree
Hide file tree
Showing 18 changed files with 573 additions and 160 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dyon"
version = "0.23.0"
version = "0.24.0"
authors = ["Sven Nilsen <bvssvni@gmail.com>"]
keywords = ["script", "scripting", "game", "language", "piston"]
description = "A rusty dynamically typed scripting language"
Expand Down
104 changes: 56 additions & 48 deletions assets/syntax.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,34 @@ _seps: "(){}[],.:;=<>*·+-/%^?~|&∧∨!¬∑∃∀\n\"\\"
201 comment = {multi_line_comment ["//" ..."\n"?]}
202 w = .r!({.w! comment})

0 fn = {
0 ns = ["ns" .w! .s!("::" .._seps!:"name")]
1 uses = .l({[.w? use:"use"] comment})
2 use = ["use" .w! .s!(["::" !"{"] .._seps!:"name")
?["::" .w? "{" .w? .s?.(,
[.._seps!:"use_fn" ?[.w! "as" .w! .._seps!:"use_fn_alias"]]
) .w? "}"]
.w! "as" .w! .._seps!:"alias"]
3 fn = {
["fn" .w! .."("!:"name" ?w "(" ?w args ?w ")" ?w ?currents ?w {
["->":"returns" ?w ?type:"ret_type"]
!"->":!"returns"
} ?w block:"block"]
[.."("!:"name" ?w "(" ?w args ?w ")" ?w ?currents ?w "=" ?w expr:"expr"]
}
1 args = .s?.(, arg:"arg")
2 arg = [?"mut":"mut" ?w .._seps!:"name" ?[?w ":" ?w
4 args = .s?.(, arg:"arg")
5 arg = [?"mut":"mut" ?w .._seps!:"name" ?[?w ":" ?w
?["'" ?w .._seps!:"lifetime"] ?w ?type:"type"]]
3 imm_arg = [!"mut " .._seps!:"name" ?[?w ":" ?w !"'" ?type:"type"]]
4 closure = ["\\(" ?w .s?.(, imm_arg:"arg") ?w ")" ?w ?currents
6 imm_arg = [!"mut " .._seps!:"name" ?[?w ":" ?w !"'" ?type:"type"]]
7 closure = ["\\(" ?w .s?.(, imm_arg:"arg") ?w ")" ?w ?currents
?w "=" ?w expr:"expr"]
5 call_closure = ["\\" item:"item" ?w "(" .s?.(, arg_expr:"call_arg") ?w ")"]
6 named_call_closure = ["\\" item:"item" ?w "(" ?w
8 call_closure = ["\\" item:"item" ?w "(" .s?.(, arg_expr:"call_arg") ?w ")"]
9 named_call_closure = ["\\" item:"item" ?w "(" ?w
.s?.(, [.._seps!:"word" ?w ":" ?w arg_expr:"call_arg" ?w]) ")"]
7 currents = ["~" ?w .s!.(, current:"current")]
8 current = [?"mut":"mut" ?w .._seps!:"name" ?[?w ":" ?w type:"type"]]
10 currents = ["~" ?w .s!.(, current:"current")]
11 current = [?"mut":"mut" ?w .._seps!:"name" ?[?w ":" ?w type:"type"]]
// Support both multi-line expressions and single line.
9 block = ["{" ?w {.l([?w expr:"expr" ?w]) [?w expr:"expr"]} ?w "}"]
10 expr = [{
12 block = ["{" ?w {.l([?w expr:"expr" ?w]) [?w expr:"expr"]} ?w "}"]
13 expr = [{
closure:"closure"
object:"object"
arr
Expand All @@ -46,7 +53,7 @@ _seps: "(){}[],.:;=<>*·+-/%^?~|&∧∨!¬∑∃∀\n\"\\"
items
} try]
// Interprets "return" as variable, does not expect loops or assignment.
11 arg_expr = {
14 arg_expr = {
["mut":"mut" ?w item:"item"]
swizzle:"swizzle"
[{
Expand All @@ -61,63 +68,64 @@ _seps: "(){}[],.:;=<>*·+-/%^?~|&∧∨!¬∑∃∀\n\"\\"
items
} try]
}
12 lexpr = [{
15 lexpr = [{
closure:"closure"
object:"object"
arr
short_loops
block:"block"
items
} try]
13 object = ["{" ?w .s?.(, key_value:"key_value") ?w "}"]
14 array = ["[" ?w .s?.(, expr:"array_item") ?w "]"]
15 array_fill = ["[" ?w expr:"fill" ?w ";" ?w expr:"n" ?w "]"]
16 key_value = [{.t?:"key" .._seps!:"key"} ?w ":" ?w expr:"val"]
17 num = .$_:"num"
18 vec4 = ["(" ?w arg_expr:"x" , ?arg_expr:"y"
16 object = ["{" ?w .s?.(, key_value:"key_value") ?w "}"]
17 array = ["[" ?w .s?.(, expr:"array_item") ?w "]"]
18 array_fill = ["[" ?w expr:"fill" ?w ";" ?w expr:"n" ?w "]"]
19 key_value = [{.t?:"key" .._seps!:"key"} ?w ":" ?w expr:"val"]
20 num = .$_:"num"
21 vec4 = ["(" ?w arg_expr:"x" , ?arg_expr:"y"
?[, arg_expr:"z" ?[, arg_expr:"w"]] ?, ?w ")"]
19 color = ["#" .._seps!:"color"]
20 text = .t?:"text"
21 bool = [{"true":"bool" "false":!"bool"} !.._seps!]
22 unop_not = [{"!":"!" "¬":"!"} ?w lexpr:"expr"]
23 unop_neg = ["-":"-" ?w mul_expr:"expr"]
24 norm = ["|" ?w expr:"expr" ?w "|"]
25 item = [?"~":"current" ?w .._seps!:"name" ?[?w "?":"try_item"]
22 color = ["#" .._seps!:"color"]
23 text = .t?:"text"
24 bool = [{"true":"bool" "false":!"bool"} !.._seps!]
25 unop_not = [{"!":"!" "¬":"!"} ?w lexpr:"expr"]
26 unop_neg = ["-":"-" ?w mul_expr:"expr"]
27 norm = ["|" ?w expr:"expr" ?w "|"]
28 item = [?"~":"current" ?w .._seps!:"name" ?[?w "?":"try_item"]
?item_extra:"item_extra"]
26 item_extra = .r!([{
29 item_extra = .r!([{
[?w "[" ?w {.t?:"id" .$_:"id" expr:"id"} ?w "]"]
[?w "." ?w .._seps!:"id"]} ?[?w "?":"try_id"]])
27 link = ["link" ?w "{" ?w link_body "}"]
28 link_body = .s?.(?w expr:"link_item")
30 link = ["link" ?w "{" ?w link_body "}"]
31 link_body = .s?.(?w expr:"link_item")
// Generate link block for body.
29 link_for = [label "link" .w! .s!.(, [.._seps!:"name" ?w
32 link_for = [label "link" .w! .s!.(, [.._seps!:"name" ?w
?{
["[" ?w expr:"start" , expr:"end" ?w ")"]
[!"{" expr:"end"]
}]) ?w "{" ?w link_body_block:"block" "}"]
30 link_body_block = link_body_expr:"expr"
31 link_body_expr = link_body:"link"
32 for = [label "for" .w!
33 link_body_block = link_body_expr:"expr"
34 link_body_expr = link_body:"link"
35 for = [label "for" .w!
expr:"init" ?w ";" ?w
expr:"cond" ?w ";" ?w
expr:"step" ?w block:"block"]
33 for_n = [label "for" short_body]
34 loop = [label "loop" .w! block:"block"]
35 break = ["break" ?w ?["'" .._seps!:"label"]]
36 continue = ["continue" ?w ?["'" .._seps!:"label"]]
37 if = ["if" .w! expr:"cond" ?w block:"true_block"
36 for_n = [label "for" short_body]
37 loop = [label "loop" .w! block:"block"]
38 break = ["break" ?w ?["'" .._seps!:"label"]]
39 continue = ["continue" ?w ?["'" .._seps!:"label"]]
40 if = ["if" .w! expr:"cond" ?w block:"true_block"
.r?([?w "else" w "if" ?w expr:"else_if_cond" ?w block:"else_if_block"])
?[?w "else" ?w block:"else_block"]]
38 call = [.._seps!:"name" wn "(" ?w .s?.(, arg_expr:"call_arg") ?w ")"]
39 named_call = [.._seps!:"word" wn "(" ?w
41 call = [?[.._seps!:"alias" "::"] .._seps!:"name" wn "(" ?w
.s?.(, arg_expr:"call_arg") ?w ")"]
42 named_call = [?[.._seps!:"alias" "::"] .._seps!:"word" wn "(" ?w
.s?.(, [.._seps!:"word" ?w ":" ?w arg_expr:"call_arg" ?w]) ")"]
40 go = ["go " ?w {call:"call" named_call:"named_call"}]
41 assign = [lexpr:"left" ?w assign_op ?w expr:"right"]
42 assign_op = {":=":":=" "=":"=" "+=":"+=" "-=":"-=" "*=":"*=" "/=":"/=" "%=":"%="}
43 compare = [lexpr:"left" ?w compare_op ?w expr:"right"]
44 compare_op = {"==":"==" "!=":"!=" "¬=":"!=" "<=":"<=" "<":"<" ">=":">=" ">":">"}
45 grab = ["grab" ?[w "'" .$:"grab_level"] w expr:"expr"]
46 try_expr = ["try" w expr:"expr"]
43 go = ["go " ?w {call:"call" named_call:"named_call"}]
44 assign = [lexpr:"left" ?w assign_op ?w expr:"right"]
45 assign_op = {":=":":=" "=":"=" "+=":"+=" "-=":"-=" "*=":"*=" "/=":"/=" "%=":"%="}
46 compare = [lexpr:"left" ?w compare_op ?w expr:"right"]
47 compare_op = {"==":"==" "!=":"!=" "¬=":"!=" "<=":"<=" "<":"<" ">=":">=" ">":">"}
48 grab = ["grab" ?[w "'" .$:"grab_level"] w expr:"expr"]
49 try_expr = ["try" w expr:"expr"]

50 label = ?["'" .._seps!:"label" ?w ":" ?w]
51 short_body = [.w! .s!.(, [.._seps!:"name" ?w
Expand Down Expand Up @@ -193,4 +201,4 @@ _seps: "(){}[],.:;=<>*·+-/%^?~|&∧∨!¬∑∃∀\n\"\\"
107 mul_expr = {mul:"mul"}
108 add = .s!({+ -} mul_expr:"expr")

1000 document = .l({[.w? fn:"fn"] comment})
1000 document = [?ns:"ns" ?w ?uses:"uses" ?w .l({[.w? fn:"fn"] comment})]
13 changes: 13 additions & 0 deletions source/namespace/graphics.dyon
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
ns graphics

fn line(a: vec4, b: vec4) -> {} {
return {
from: clone(a),
to: clone(b),
}
}

fn add(a: f64, b: f64) -> f64 {
println("boo")
return a + b
}
11 changes: 11 additions & 0 deletions source/namespace/loader.dyon
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
ns loader

fn main() {
math := unwrap(load("source/namespace/math.dyon"))
graphics := unwrap(load("source/namespace/graphics.dyon"))
main := unwrap(load(
source: "source/namespace/main.dyon",
imports: [math, graphics]
))
call(main, "main", [])
}
11 changes: 11 additions & 0 deletions source/namespace/main.dyon
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
ns program

use math::algebra::{add as plus} as m
use graphics::{line} as m

fn main() {
bar(m::plus(1, 2))
println(m::line((0, 0), (1, 1)))
}

fn bar(a: f64) {}
9 changes: 9 additions & 0 deletions source/namespace/math.dyon
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
ns math::algebra

fn add(a: f64, b: f64) -> f64 {
return a + b
}

fn sub(a: f64, b: f64) -> f64 {
return a - b
}
6 changes: 2 additions & 4 deletions source/test.dyon
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
fn main() {
fail := unwrap(load("source/data/fail.dyon"))
success := unwrap(load("source/data/success.dyon"))
call(fail, "main", [])
call(success, "main", [])
loader := unwrap(load("source/namespace/loader.dyon"))
call(loader, "main", [])
}
1 change: 1 addition & 0 deletions src/ast/infer_len.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub fn infer(block: &Block, name: &str) -> Option<Expression> {
let res = list.map(|item| {
let source_range = item.source_range;
Expression::Call(Call {
alias: None,
name: Arc::new("len".into()),
f_index: Cell::new(FnIndex::None),
args: vec![
Expand Down
Loading

0 comments on commit 662d943

Please sign in to comment.