diff --git a/packages/core/error.huia b/packages/core/error.huia new file mode 100644 index 0000000..25a12a2 --- /dev/null +++ b/packages/core/error.huia @@ -0,0 +1,7 @@ +deftype Error message: String, backtrace: Array, as: -> + + defstatic new message: String, backtrace: Array, as: -> + Error { message: message, backtrace: backtrace } + end + +end diff --git a/packages/core/error/option.huia b/packages/core/error/option.huia new file mode 100644 index 0000000..c50dc1c --- /dev/null +++ b/packages/core/error/option.huia @@ -0,0 +1,14 @@ +defimpl Option for: Error, as: -> + + def some? as: -> false end + def none? as: -> true end + + def unwrap as: -> + Huia.Primitive.panic("Attempt to unwrap Error") + end + + def expect message: String, as: -> + Huia.Primitive.panic(message) + end + +end diff --git a/packages/core/error/result.huia b/packages/core/error/result.huia new file mode 100644 index 0000000..5805177 --- /dev/null +++ b/packages/core/error/result.huia @@ -0,0 +1,14 @@ +defimpl Result for: Error, as: -> + + def ok? as: -> false end + def error? as: -> true end + + def ok as: -> + Huia.Primitive.panic("Attempt to convert error into value") + end + + def error as: -> + self + end + +end diff --git a/packages/core/float.huia b/packages/core/float.huia index 12ff589..2247052 100644 --- a/packages/core/float.huia +++ b/packages/core/float.huia @@ -1,4 +1,4 @@ -deftrait Integer requires: [ +deftrait Float requires: [ Addition, Division, Equality, diff --git a/packages/core/native/float_64/addition.huia b/packages/core/native/float_64/addition.huia index 015c853..113a18e 100644 --- a/packages/core/native/float_64/addition.huia +++ b/packages/core/native/float_64/addition.huia @@ -1,12 +1,12 @@ defimpl Addition for: Native.Float64, as: -> def add other: Native.Integer64, as: -> - as_float = Huia.Primitive.i64_to_f64!(value: other) - Huia.Primitive.f64_addition(rhs: self, lhs: as_float) + as_float = Huia.Primitive.i64_to_f64!(other) + Huia.Primitive.f64_addition(self, as_float) end def add other: Native.Float64, as: -> - Huia.Primitive.f64_addition(rhs: self, lhs: other) + Huia.Primitive.f64_addition(self, other) end def add other: Any, as: -> diff --git a/packages/core/native/float_64/division.huia b/packages/core/native/float_64/division.huia index 42a1c30..02c3b3b 100644 --- a/packages/core/native/float_64/division.huia +++ b/packages/core/native/float_64/division.huia @@ -1,12 +1,12 @@ defimpl Division for: Native.Float64, as: -> def divide_by other: Native.Float64, as: -> - Huia.Primitive.f64_division(lhs: self, rhs: other) + Huia.Primitive.f64_division(self, other) end def divide_by other: Native.Integer64, as: -> - as_float = Huia.Primitive.i64_to_f64!(value: other) - Huia.Primitive.f64_division(lhs: self, rhs: as_float) + as_float = Huia.Primitive.i64_to_f64!(other) + Huia.Primitive.f64_division(self, as_float) end def divide_by other: Any, as: -> diff --git a/packages/core/native/float_64/equality.huia b/packages/core/native/float_64/equality.huia index b488c86..e8a55fc 100644 --- a/packages/core/native/float_64/equality.huia +++ b/packages/core/native/float_64/equality.huia @@ -1,7 +1,7 @@ defimpl Equality for: Native.Float64, as: -> def equals? other: Native.Float64, as: -> - Huia.Primitive.f64_equals(lhs: self, rhs: other) + Huia.Primitive.f64_equals(self, other) end def equals? other: Any, as: -> false diff --git a/packages/core/native/float_64/multiplication.huia b/packages/core/native/float_64/multiplication.huia index 991f651..490b1d9 100644 --- a/packages/core/native/float_64/multiplication.huia +++ b/packages/core/native/float_64/multiplication.huia @@ -1,12 +1,12 @@ defimpl Multiplication for: Native.Float64, as: -> def multiply_by other: Native.Float64, as: -> - Huia.Primitive.f64_multiply_by(lhs: self, rhs: other) + Huia.Primitive.f64_multiply_by(self, other) end def multiply_by other: Native.Integer64, as: -> - as_float = Huia.Primitive.i64_to_f64!(value: other) - Huia.Primitive.f64_multiply_by(lhs: self, rhs: as_float) + as_float = Huia.Primitive.i64_to_f64!(other) + Huia.Primitive.f64_multiply_by(self, as_float) end def multiply_by other: Any, as: -> diff --git a/packages/core/native/integer_64/addition.huia b/packages/core/native/integer_64/addition.huia index bb5e957..fb317d2 100644 --- a/packages/core/native/integer_64/addition.huia +++ b/packages/core/native/integer_64/addition.huia @@ -1,12 +1,12 @@ defimpl Addition for: Native.Integer64, as: -> def add other: Native.Integer64, as: -> - Huia.Primitive.i64_addition(lhs: self, rhs: other) + Huia.Primitive.i64_addition(self, other) end def add other: Native.Float64, as: -> - as_float = Huia.Primitive.i64_to_f64!(value: self) - Huia.Primitive.f64_addition(lhs: as_float, rhs: other) + as_float = Huia.Primitive.i64_to_f64!(self) + Huia.Primitive.f64_addition(as_float, other) end def add other: Any, as: -> diff --git a/packages/core/native/integer_64/bitwise_and.huia b/packages/core/native/integer_64/bitwise_and.huia index ed4ba6c..3183d71 100644 --- a/packages/core/native/integer_64/bitwise_and.huia +++ b/packages/core/native/integer_64/bitwise_and.huia @@ -1,7 +1,7 @@ defimpl BitwiseAnd for: Native.Integer64, as: -> def bitwise_and other: Native.Integer64, as: -> - Huia.Primitive.i64_bitwise_and(lhs: self, rhs: other) + Huia.Primitive.i64_bitwise_and(self, other) end def bitwise_and other: Any, as: -> diff --git a/packages/core/native/integer_64/bitwise_or.huia b/packages/core/native/integer_64/bitwise_or.huia index 0949033..db9afcd 100644 --- a/packages/core/native/integer_64/bitwise_or.huia +++ b/packages/core/native/integer_64/bitwise_or.huia @@ -1,7 +1,7 @@ defimpl BitwiseOr for: Native.Integer64, as: -> def bitwise_or other: Native.Integer64, as: -> - Huia.Primitive.i64_bitwise_or(lhs: self, rhs: other) + Huia.Primitive.i64_bitwise_or(self, other) end def bitwise_or other: Any, as: -> diff --git a/packages/core/native/integer_64/bitwise_xor.huia b/packages/core/native/integer_64/bitwise_xor.huia index 48c01e4..7da4acd 100644 --- a/packages/core/native/integer_64/bitwise_xor.huia +++ b/packages/core/native/integer_64/bitwise_xor.huia @@ -1,7 +1,7 @@ defimpl BitwiseXor for: Native.Integer64, as: -> def bitwise_xor other: Native.Integer64, as: -> - Huia.Primitive.i64_bitwise_xor(lhs: self, rhs: other) + Huia.Primitive.i64_bitwise_xor(self, other) end def bitwise_xor other: Any, as: -> diff --git a/packages/core/native/integer_64/division.huia b/packages/core/native/integer_64/division.huia index 02ab050..78dacf1 100644 --- a/packages/core/native/integer_64/division.huia +++ b/packages/core/native/integer_64/division.huia @@ -1,12 +1,12 @@ defimpl Division for: Native.Integer64, as: -> def divide_by other: Native.Integer64, as: -> - Huia.Primitive.i64_division(lhs: self, rhs: other) + Huia.Primitive.i64_division(self, other) end def divide_by other: Native.Float64, as: -> - as_float = Huia.Primitive.i64_to_f64!(value: self) - Huia.Primitive.f64_division(lhs: as_float, rhs: other) + as_float = Huia.Primitive.i64_to_f64!(self) + Huia.Primitive.f64_division(as_float, other) end def divide_by other: Any, as: -> diff --git a/packages/core/native/integer_64/equality.huia b/packages/core/native/integer_64/equality.huia index bde292b..5b0e01a 100644 --- a/packages/core/native/integer_64/equality.huia +++ b/packages/core/native/integer_64/equality.huia @@ -1,7 +1,7 @@ defimpl Equality for: Native.Integer64, as: -> def equals? other: Native.Integer64, as: -> - Huia.Primitive.i64_equals(lhs: self, rhs: other) + Huia.Primitive.i64_equals(self, other) end def equals? other: Any, as: -> false diff --git a/packages/core/native/integer_64/exponentiation.huia b/packages/core/native/integer_64/exponentiation.huia index 96ececc..1512c34 100644 --- a/packages/core/native/integer_64/exponentiation.huia +++ b/packages/core/native/integer_64/exponentiation.huia @@ -1,12 +1,12 @@ defimpl Exponentiation for: Native.Integer64, as: -> def exponent_of other: Native.Integer64, as: -> - Huia.Primitive.i64_expo(lhs: self, rhs: other) + Huia.Primitive.i64_expo(self, other) end def exponent_of other: Native.Float64, as: -> - as_float = Huia.Primitive.i64_to_f64!(value: self) - Native.Primitive.f64_expo(lhs: self, rhs: other) + as_float = Huia.Primitive.i64_to_f64!(self) + Native.Primitive.f64_expo(self, other) end def exponent_of other: Any, as: -> diff --git a/packages/core/native/integer_64/logical_and.huia b/packages/core/native/integer_64/logical_and.huia index ecc17c1..249e1d7 100644 --- a/packages/core/native/integer_64/logical_and.huia +++ b/packages/core/native/integer_64/logical_and.huia @@ -1,7 +1,7 @@ defimpl LogicalAnd for: Native.Integer64, as: -> def logical_and other: Native.Integer64, as: -> - Huia.Primitive.i64_logical_and(lhs: self, other: other) + Huia.Primitive.i64_logical_and(self, other) end def logical_and other: Any, as: -> diff --git a/packages/core/native/integer_64/logical_not.huia b/packages/core/native/integer_64/logical_not.huia index bb01e58..4756ad9 100644 --- a/packages/core/native/integer_64/logical_not.huia +++ b/packages/core/native/integer_64/logical_not.huia @@ -1,7 +1,7 @@ defimpl LogicalNot for: Native.Integer64, as: -> def logical_and other: Native.Integer64, as: -> - Huia.Primitive.i64_logical_and(lhs: self, other: other) + Huia.Primitive.i64_logical_and(self, other) end def logical_and other: Any, as: -> diff --git a/packages/core/native/integer_64/logical_or.huia b/packages/core/native/integer_64/logical_or.huia index bf4677e..b233849 100644 --- a/packages/core/native/integer_64/logical_or.huia +++ b/packages/core/native/integer_64/logical_or.huia @@ -1,7 +1,7 @@ defimpl LogicalOr for: Native.Integer64, as: -> def logical_or other: Native.Integer64, as: -> - Huia.Primitive.i64_logical_or(lhs: self, rhs: other) + Huia.Primitive.i64_logical_or(self, other) end def logical_or other: Any, as: -> diff --git a/packages/core/native/integer_64/modulus.huia b/packages/core/native/integer_64/modulus.huia index 92d02c4..48cd5a7 100644 --- a/packages/core/native/integer_64/modulus.huia +++ b/packages/core/native/integer_64/modulus.huia @@ -1,7 +1,7 @@ defimpl Modulus for: Native.Integer64, as: -> def modulo other: Native.Integer64, as: -> - Huia.Primitive.i64_modulo(lhs: self, rhs: other) + Huia.Primitive.i64_modulo(self, other) end def modulo other: Any, as: -> diff --git a/packages/core/native/integer_64/multiplication.huia b/packages/core/native/integer_64/multiplication.huia index 1f4dd19..865a824 100644 --- a/packages/core/native/integer_64/multiplication.huia +++ b/packages/core/native/integer_64/multiplication.huia @@ -1,12 +1,12 @@ defimpl Multiplication for: Native.Integer64, as: -> def multiply_by other: Native.Integer64, as: -> - Huia.Primitive.i64_multiply_by(lhs: self, rhs: other) + Huia.Primitive.i64_multiply_by(self, other) end def multiply_by other: Native.Float64, as: -> - as_float = Huia.Primitive.i64_to_f64!(value: self) - Huia.Primitive.f64_multiply_by(lhs: as_float, rhs: other) + as_float = Huia.Primitive.i64_to_f64!(self) + Huia.Primitive.f64_multiply_by(as_float, other) end def multiply_by other: Any, as: -> diff --git a/packages/core/native/integer_64/ordering.huia b/packages/core/native/integer_64/ordering.huia index 0cf34b1..df4a478 100644 --- a/packages/core/native/integer_64/ordering.huia +++ b/packages/core/native/integer_64/ordering.huia @@ -1,14 +1,14 @@ defimpl Ordering for: Native.Integer64, as: -> def compare_with other: Native.Integer64, as: -> - match Huia.Primitive.i64_compare(lhs: self, rhs: other), + match Huia.Primitive.i64_compare(self, other), -1: -> Ordering.Less.new() end, 0: -> Ordering.Equal.new() end, 1: -> Ordering.Greater.new() end end def equals? other: Native.Integer64, as: -> - match Huia.Primitive.i64_compare(lhs: self, rhs: other), + match Huia.Primitive.i64_compare(self, other), 0: -> true end, Any: -> false end end @@ -16,14 +16,14 @@ defimpl Ordering for: Native.Integer64, as: -> def equals? other: Any, as: -> false def greater_than? other: Native.Integer64, as: -> - match Huia.Primitive.i64_compare(lhs: self, rhs: other), + match Huia.Primitive.i64_compare(self, other), 1: -> true end, Any: -> false end end def greater_than? other: Native.Float64, as: -> - as_float = Huia.Primitive.i64_to_f64!(value: self) - match Huia.Primitive.f64_compare(lhs: as_float, rhs: other), + as_float = Huia.Primitive.i64_to_f64!(self) + match Huia.Primitive.f64_compare(as_float, other), 1: -> true end, Any: -> false end end @@ -31,15 +31,15 @@ defimpl Ordering for: Native.Integer64, as: -> def greater_than? other: Any, as: -> false def greater_than_or_equal_to? other: Native.Integer64, as: -> - match Huia.Primitive.i64_compare(lhs: self, rhs: other), + match Huia.Primitive.i64_compare(self, other), 1: -> true end, 0: -> true end, Any: -> false end end def greater_than_or_equal_to? other: Native.Float64, as: -> - as_float = Huia.Primitive.i64_to_f64!(value: self) - match Huia.Primitive.f64_compare(lhs: as_float, rhs: other), + as_float = Huia.Primitive.i64_to_f64!(self) + match Huia.Primitive.f64_compare(as_float, other), 1: -> true end, Any: -> false end end @@ -47,14 +47,14 @@ defimpl Ordering for: Native.Integer64, as: -> def greater_than_or_equal_to? other: Any, as: -> false def less_than? other: Native.Integer64, as: -> - match Huia.Primitive.i64_compare(lhs: self, rhs: other), + match Huia.Primitive.i64_compare(self, other), -1: -> true end, Any: -> false end end def less_than? other: Native.Float64, as: -> - as_float = Huia.Primitive.i64_to_f64!(value: self) - match Huia.Primitive.f64_compare(lhs: as_float, rhs: other), + as_float = Huia.Primitive.i64_to_f64!(self) + match Huia.Primitive.f64_compare(as_float, other), -1: -> true end, Any: -> false end end @@ -62,15 +62,15 @@ defimpl Ordering for: Native.Integer64, as: -> def less_than? other: Any, as: -> false def less_than_or_equal_to? other: Native.Integer64, as: -> - match Huia.Primitive.i64_compare(lhs: self, rhs: other), + match Huia.Primitive.i64_compare(self, other), -1: -> true end, 0: -> true end, Any: -> false end end def less_than_or_equal_to? other: Native.Float64, as: -> - as_float = Huia.Primitive.i64_to_f64!(value: self) - match Huia.Primitive.f64_compare(lhs: as_float, rhs: other), + as_float = Huia.Primitive.i64_to_f64!(self) + match Huia.Primitive.f64_compare(as_float, other), -1: -> true end, Any: -> false end end diff --git a/packages/core/none.huia b/packages/core/none.huia new file mode 100644 index 0000000..c2ca25c --- /dev/null +++ b/packages/core/none.huia @@ -0,0 +1,7 @@ +deftype None as: -> + + defstatic new as: -> + None { } + end + +end diff --git a/packages/core/none/option.huia b/packages/core/none/option.huia new file mode 100644 index 0000000..fb97bfa --- /dev/null +++ b/packages/core/none/option.huia @@ -0,0 +1,14 @@ +defimpl Option for: None, as: -> + + def some? as: -> false end + def none? as: -> true end + + def unwrap as: -> + Huia.Primitive.panic("Attempt to unwrap None") + end + + def expect message: String, as: -> + Huia.Primitive.panic(message) + end + +end diff --git a/packages/core/ok.huia b/packages/core/ok.huia new file mode 100644 index 0000000..db9129a --- /dev/null +++ b/packages/core/ok.huia @@ -0,0 +1,7 @@ +deftype Ok value: Any, as: -> + + defstatic new value: Any, as: -> + Ok { value: value } + end + +end diff --git a/packages/core/ok/option.huia b/packages/core/ok/option.huia new file mode 100644 index 0000000..11e69c2 --- /dev/null +++ b/packages/core/ok/option.huia @@ -0,0 +1,14 @@ +defimpl Option for: Ok, as: -> + + def some? as: -> true end + def none? as: -> false end + + def unwrap as: -> + @value + end + + def expect message: String, as: -> + @value + end + +end diff --git a/packages/core/ok/result.huia b/packages/core/ok/result.huia new file mode 100644 index 0000000..a071c49 --- /dev/null +++ b/packages/core/ok/result.huia @@ -0,0 +1,14 @@ +defimpl Result for: Ok, as: -> + + def ok? as: -> true end + def error? as: -> false end + + def ok as: -> + @value + end + + def error as: -> + Huia.Primitive.panic("Attempt to access error from Ok") + end + +end diff --git a/packages/core/option.huia b/packages/core/option.huia new file mode 100644 index 0000000..e4b6232 --- /dev/null +++ b/packages/core/option.huia @@ -0,0 +1,9 @@ +deftrait Option as: -> + + def some? + def none? + + def unwrap + def expect message: String + +end diff --git a/packages/core/result.huia b/packages/core/result.huia index 2e05a2a..f14bfaa 100644 --- a/packages/core/result.huia +++ b/packages/core/result.huia @@ -1,13 +1,9 @@ deftrait Result as: -> - defstatic ok value: Any, as: -> Result.Ok.new(value) - defstatic error error: Error, as: -> Result.Error.new(error) - defstatic none as: -> Result.None.new() - def ok? def error? - def unwrap - def expect message: String + def ok + def error end diff --git a/packages/core/result/error.huia b/packages/core/result/error.huia deleted file mode 100644 index 833b701..0000000 --- a/packages/core/result/error.huia +++ /dev/null @@ -1,20 +0,0 @@ -deftype Result.Error error: Error, as: -> - - defstatic new error: Error, as: -> - Result.Error{ error: error } - end - -end - -defimpl Result for: Result.Error, as: -> - def ok? as: -> false - def error? as: -> true - - def unwrap as: -> - Huia.Primitive.panic_process(@error) - end - - def expect message: String, as: -> - Huia.Primitive.panic_process(Error.new(message: message, cause: @error)) - end -end diff --git a/packages/core/result/none.huia b/packages/core/result/none.huia deleted file mode 100644 index f9d5e68..0000000 --- a/packages/core/result/none.huia +++ /dev/null @@ -1,20 +0,0 @@ -deftype Result.None value: Any, as: -> - - defstatic new as: -> - Result.None { } - end - -end - -defimpl Result for: Result.None, as: -> - def ok? as: -> true - def error? as: -> false - - def unwrap as: -> - Huia.Primitive.panic_process("Attempt to unwrap none") - end - - def expect message: String, as: -> - Huia.Primitive.panic_process(Error.new(message: message)) - end -end diff --git a/packages/core/result/ok.huia b/packages/core/result/ok.huia deleted file mode 100644 index 8a043b6..0000000 --- a/packages/core/result/ok.huia +++ /dev/null @@ -1,14 +0,0 @@ -deftype Result.Ok value: Any, as: -> - - defstatic new value: Any, as: -> - Result.Ok { value: value } - end - -end - -defimpl Result, for: Result.Ok, as: -> - def ok? as: -> true - def error? as: -> false - def unwrap as: -> @value - def expect as: -> @value -end diff --git a/packages/core/some.huia b/packages/core/some.huia new file mode 100644 index 0000000..37a3bf0 --- /dev/null +++ b/packages/core/some.huia @@ -0,0 +1,7 @@ +deftype Some value: Any, as: -> + + defstatic new value: Any, as: -> + Some { value: value } + end + +end diff --git a/packages/core/some/option.huia b/packages/core/some/option.huia new file mode 100644 index 0000000..d8d3d30 --- /dev/null +++ b/packages/core/some/option.huia @@ -0,0 +1,14 @@ +defimpl Option for: Some, as: -> + + def some? as: -> true end + def none? as: -> false end + + def unwrap as: -> + @value + end + + def expect message: String, as: -> + @value + end + +end diff --git a/parser/src/grammar.pest b/parser/src/grammar.pest index a81bb3e..a8d8c9d 100644 --- a/parser/src/grammar.pest +++ b/parser/src/grammar.pest @@ -21,7 +21,7 @@ file = _{ soi ~ def* ~ eoi } // #################################### expression_test = _{ soi ~ expression ~ eoi } -expression = _{ literal ~ (binary_operator ~ literal)* } +expression = _{ literal ~ (whitespace* ~ binary_operator ~ whitespace* ~ literal)* } // #################################### @@ -32,7 +32,7 @@ atom = ${ ":" ~ identifier } boolean = _{ boolean_true | boolean_false } boolean_true = { "true" } boolean_false = { "false" } -literal = _{ unary | constructor | list | integer | float | string | atom | property | identifier | braced | boolean } +literal = _{ unary | call | constructor | list | integer | float | string | atom | property | variable | braced | boolean } braced = !{ "(" ~ expression ~ ")" } list = !{ "[" ~ (expression ~ ("," ~ expression)*)? ~ "]" } unary = ${ unary_operator ~ literal } @@ -67,11 +67,10 @@ identifier_tail = @{ ('a'..'z' | 'A'..'Z' | "_" | '0'..'9')+ } // #################################### block_test = _{ soi ~ block ~ eoi } -block = @{ block_stabby ~ space* ~ (block_multi_line | block_single_line) } +block = @{ block_stabby ~ whitespace+ ~ (block_end | block_body) } block_stabby = _{ "->" } -block_single_line = _{ block_end | block_single_expression } -block_single_expression = _{ expression ~ space+ ~ block_end } -block_multi_line = _{ padded_newline+ ~ ((def | expression) ~ padded_newline+)* ~ block_end } +block_body = _{ block_expression ~ (padded_newline+ ~ block_expression)* ~ whitespace* ~ block_end } +block_expression = _{ def | expression } block_end = _{ "end" ~ !identifier_tail } @@ -86,7 +85,7 @@ integer_hexadecimal = { ('0'..'9' | 'a'..'f' | 'A'..'F') ~ ('0'..'9' | integer_hexadecimal_sigil = _{ "0x" ~ integer_hexadecimal } integer_octal = { ('0'..'7') ~ ('0'..'7' | "_" )* } integer_octal_sigil = _{ "0o" ~ integer_octal } -integer_binary = { ("0" | "1") ~ ("0" | "1" | "_")* } +integer_binary = { ('0'..'1') ~ ('0'..'1' | "_")* } integer_binary_sigil = _{ "0b" ~ integer_binary } integer_zero = { "0" } @@ -119,9 +118,9 @@ string_escape = _{ "\\" ~ ("\"" | "\\" | "/" | "b" | "f" | "n" | " // #################################### argument_list = _{ argument_pair ~ ("," ~ argument_pair)* } -argument_pair = { argument_keyword ~ argument_value } +argument_pair = !{ argument_keyword ~ argument_value } argument_keyword = ${ identifier ~ ":" } -argument_value = _{ block | def_fn | expression } +argument_value = _{ block | expression } // #################################### @@ -138,7 +137,10 @@ def_public = { "def" } def_trait = { "deftrait" } def_type = { "deftype" } def_impl = { "defimpl" } -def_name = { identifier } +def_name = ${ identifier ~ def_modifier? } +def_predicate = { "?" } +def_bang = { "!" } +def_modifier = _{ def_predicate | def_bang } def_return_type = { "<" ~ identifier ~ ">" } def_fn = _{ def_fn_keyword ~ def_return_type? ~ def_name ~ argument_list? } def_ty = _{ def_type_keyword ~ def_name ~ argument_list? } @@ -182,3 +184,14 @@ binary_operator = { } unary_operator = { plus | minus | logical_not } + +// #################################### +// ## METHOD CALLS +// #################################### + +call_test = _{ soi ~ call ~ eoi } +call = ${ call_receiver ~ "." ~ call_name ~ call_arguments } +call_receiver = { identifier } +call_name = { identifier } +call_arguments = !{ "(" ~ (call_argument ~ ("," ~ call_argument)*)? ~ ")" } +call_argument = { expression } diff --git a/parser/src/grammar/test/acceptance.huia b/parser/src/grammar/test/acceptance.huia index 358d9ae..b077cec 100644 --- a/parser/src/grammar/test/acceptance.huia +++ b/parser/src/grammar/test/acceptance.huia @@ -1,7 +1,9 @@ deftype Delorean speed: Integer, as: -> + defstatic new as: -> Delorean { speed: 0 } end + end deftrait Car as: -> @@ -19,5 +21,5 @@ defimpl Car for: Delorean, as: -> end defimpl TimeMachine for: Delorean, as: -> - def travelling_through_time? as: -> @speed >= 88 + def travelling_through_time? as: -> @speed >= 88 end end diff --git a/parser/src/grammar/test/block.rs b/parser/src/grammar/test/block.rs index 29f2a5a..fb009ef 100644 --- a/parser/src/grammar/test/block.rs +++ b/parser/src/grammar/test/block.rs @@ -47,7 +47,7 @@ fn multi_expression() { } #[test] -fn constructor() { +fn with_constructor() { parses_to!( parser: Grammar, input: r#"-> diff --git a/parser/src/grammar/test/call.rs b/parser/src/grammar/test/call.rs new file mode 100644 index 0000000..cdd235d --- /dev/null +++ b/parser/src/grammar/test/call.rs @@ -0,0 +1,36 @@ +use grammar::{Grammar, Rule}; + +#[test] +fn call_no_args() { + parses_to!( + parser: Grammar, + input: "marty.mcfly( )", + rule: Rule::call_test, + tokens: [ + call(0, 14, [ + call_receiver(0, 5, [identifier(0, 5)]), + call_name(6, 11, [identifier(6, 11)]), + call_arguments(11, 14) + ]) + ] + ) +} + +#[test] +fn call_with_args() { + parses_to!( + parser: Grammar, + input: "marty.mcfly( :hello, 88 )", + rule: Rule::call_test, + tokens: [ + call(0, 25, [ + call_receiver(0, 5, [identifier(0, 5)]), + call_name(6, 11, [identifier(6, 11)]), + call_arguments(11, 25, [ + call_argument(13, 19, [atom(13, 19, [identifier(14, 19)])]), + call_argument(21, 24, [integer(21, 23, [integer_decimal(21, 23)])]) + ]), + ]) + ] + ) +} diff --git a/parser/src/grammar/test/def.rs b/parser/src/grammar/test/def.rs index 3fb27a6..1e6b7fb 100644 --- a/parser/src/grammar/test/def.rs +++ b/parser/src/grammar/test/def.rs @@ -48,7 +48,7 @@ fn define_return_type_with_args() { def_name(16, 29, [identifier(16, 29)]), argument_pair(30, 42, [ argument_keyword(30, 35, [identifier(30, 34)]), - identifier(36, 42) + variable(36, 42, [identifier(36, 42)]) ]), argument_pair(44, 51, [ argument_keyword(44, 47, [identifier(44, 46)]), @@ -71,7 +71,7 @@ fn define_with_args() { def_name(4, 17, [identifier(4, 17)]), argument_pair(18, 30, [ argument_keyword(18, 23, [identifier(18, 22)]), - identifier(24, 30) + variable(24, 30, [identifier(24, 30)]) ]), argument_pair(32, 39, [ argument_keyword(32, 35, [identifier(32, 34)]), @@ -163,8 +163,8 @@ fn def_trait_with_args() { identifier(17, 25) ]), list(27, 49, [ - identifier(28, 36), - identifier(38, 48) + variable(28, 36, [identifier(28, 36)]), + variable(38, 48, [identifier(38, 48)]) ]) ]) ]) diff --git a/parser/src/grammar/test/mod.rs b/parser/src/grammar/test/mod.rs index c4602b0..6cd1175 100644 --- a/parser/src/grammar/test/mod.rs +++ b/parser/src/grammar/test/mod.rs @@ -1,6 +1,7 @@ mod acceptance; mod binary; mod block; +mod call; mod def; mod float; mod identifier;