Working verison 0 parser.

This commit is contained in:
James Harton 2018-09-01 11:44:59 +12:00
parent b5a3d088d1
commit ec5cdc8afc
39 changed files with 236 additions and 121 deletions

7
packages/core/error.huia Normal file
View file

@ -0,0 +1,7 @@
deftype Error message: String, backtrace: Array, as: ->
defstatic <Error> new message: String, backtrace: Array, as: ->
Error { message: message, backtrace: backtrace }
end
end

View file

@ -0,0 +1,14 @@
defimpl Option for: Error, as: ->
def some? as: -> false end
def none? as: -> true end
def <Any> unwrap as: ->
Huia.Primitive.panic("Attempt to unwrap Error")
end
def <Any> expect message: String, as: ->
Huia.Primitive.panic(message)
end
end

View file

@ -0,0 +1,14 @@
defimpl Result for: Error, as: ->
def ok? as: -> false end
def error? as: -> true end
def <Any> ok as: ->
Huia.Primitive.panic("Attempt to convert error into value")
end
def <Any> error as: ->
self
end
end

View file

@ -1,4 +1,4 @@
deftrait Integer requires: [
deftrait Float requires: [
Addition,
Division,
Equality,

View file

@ -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: ->

View file

@ -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: ->

View file

@ -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

View file

@ -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: ->

View file

@ -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: ->

View file

@ -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: ->

View file

@ -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: ->

View file

@ -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: ->

View file

@ -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: ->

View file

@ -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

View file

@ -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: ->

View file

@ -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: ->

View file

@ -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: ->

View file

@ -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: ->

View file

@ -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: ->

View file

@ -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: ->

View file

@ -1,14 +1,14 @@
defimpl Ordering for: Native.Integer64, as: ->
def <Ordering.Result> 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

7
packages/core/none.huia Normal file
View file

@ -0,0 +1,7 @@
deftype None as: ->
defstatic <None> new as: ->
None { }
end
end

View file

@ -0,0 +1,14 @@
defimpl Option for: None, as: ->
def some? as: -> false end
def none? as: -> true end
def <Any> unwrap as: ->
Huia.Primitive.panic("Attempt to unwrap None")
end
def <Any> expect message: String, as: ->
Huia.Primitive.panic(message)
end
end

7
packages/core/ok.huia Normal file
View file

@ -0,0 +1,7 @@
deftype Ok value: Any, as: ->
defstatic <Ok> new value: Any, as: ->
Ok { value: value }
end
end

View file

@ -0,0 +1,14 @@
defimpl Option for: Ok, as: ->
def some? as: -> true end
def none? as: -> false end
def <Any> unwrap as: ->
@value
end
def <Any> expect message: String, as: ->
@value
end
end

View file

@ -0,0 +1,14 @@
defimpl Result for: Ok, as: ->
def ok? as: -> true end
def error? as: -> false end
def <Any> ok as: ->
@value
end
def <Any> error as: ->
Huia.Primitive.panic("Attempt to access error from Ok")
end
end

View file

@ -0,0 +1,9 @@
deftrait Option as: ->
def some?
def none?
def <Any> unwrap
def <Any> expect message: String
end

View file

@ -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 <Any> unwrap
def <Any> expect message: String
def <Any> ok
def <Any> error
end

View file

@ -1,20 +0,0 @@
deftype Result.Error error: Error, as: ->
defstatic <Result.Error> 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 <Any> unwrap as: ->
Huia.Primitive.panic_process(@error)
end
def <Any> expect message: String, as: ->
Huia.Primitive.panic_process(Error.new(message: message, cause: @error))
end
end

View file

@ -1,20 +0,0 @@
deftype Result.None value: Any, as: ->
defstatic <Result.None> new as: ->
Result.None { }
end
end
defimpl Result for: Result.None, as: ->
def ok? as: -> true
def error? as: -> false
def <Any> unwrap as: ->
Huia.Primitive.panic_process("Attempt to unwrap none")
end
def <Any> expect message: String, as: ->
Huia.Primitive.panic_process(Error.new(message: message))
end
end

View file

@ -1,14 +0,0 @@
deftype Result.Ok value: Any, as: ->
defstatic <Result.Ok> 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 <Any> unwrap as: -> @value
def <Any> expect as: -> @value
end

7
packages/core/some.huia Normal file
View file

@ -0,0 +1,7 @@
deftype Some value: Any, as: ->
defstatic <Some> new value: Any, as: ->
Some { value: value }
end
end

View file

@ -0,0 +1,14 @@
defimpl Option for: Some, as: ->
def some? as: -> true end
def none? as: -> false end
def <Any> unwrap as: ->
@value
end
def <Any> expect message: String, as: ->
@value
end
end

View file

@ -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 }

View file

@ -1,7 +1,9 @@
deftype Delorean speed: Integer, as: ->
defstatic <Delorean> 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

View file

@ -47,7 +47,7 @@ fn multi_expression() {
}
#[test]
fn constructor() {
fn with_constructor() {
parses_to!(
parser: Grammar,
input: r#"->

View file

@ -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)])])
]),
])
]
)
}

View file

@ -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)])
])
])
])

View file

@ -1,6 +1,7 @@
mod acceptance;
mod binary;
mod block;
mod call;
mod def;
mod float;
mod identifier;