mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-05 22:48:42 +00:00
operator prescendence added
This commit is contained in:
parent
93ac5f8e33
commit
e62144feac
11 changed files with 328 additions and 26 deletions
|
|
@ -1 +1 @@
|
||||||
Subproject commit 82f3a4edebad870fbf7d28bfd06bcc67731d5837
|
Subproject commit ca9eac9857bb2e8276408fe4c0460d171485c569
|
||||||
|
|
@ -872,6 +872,13 @@ void BuildVisitor::Visit(BinaryOperatorExpression* node) {
|
||||||
|
|
||||||
node->operator_name = parse_node.ChildByFieldName("operator_name").GetValue();
|
node->operator_name = parse_node.ChildByFieldName("operator_name").GetValue();
|
||||||
|
|
||||||
|
{ // remove operator prescendence markers
|
||||||
|
size_t operator_size = 0;
|
||||||
|
for (; operator_size < node->operator_name.size() && node->operator_name[operator_size] != '.'; ++operator_size) {}
|
||||||
|
|
||||||
|
node->operator_name = node->operator_name.substr(0, operator_size);
|
||||||
|
}
|
||||||
|
|
||||||
current_node_ = parse_node.ChildByFieldName("right_expression");
|
current_node_ = parse_node.ChildByFieldName("right_expression");
|
||||||
Visit(node->right_expression);
|
Visit(node->right_expression);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ def test_arrays = {
|
||||||
var arr1 = ,1 ,2 ,3
|
var arr1 = ,1 ,2 ,3
|
||||||
const arr2 = Int._array: 32
|
const arr2 = Int._array: 32
|
||||||
var arr3 = String._array: 11
|
var arr3 = String._array: 11
|
||||||
const arr4 = 'a'..'z'
|
const arr4 = 'a'--'z'
|
||||||
const n = 100
|
const n = 100
|
||||||
var @arr5 <- Int.@_new_array: 10
|
var @arr5 <- Int.@_new_array: 10
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
namespace Employee {
|
namespace Employee {
|
||||||
decl gen_employee : Unit -> Employee
|
decl gen_employee : Unit -> Employee
|
||||||
def gen_employee : a = {
|
def gen_employee = {
|
||||||
return
|
return
|
||||||
$Employee
|
$Employee
|
||||||
& name = "John"
|
& name = "John"
|
||||||
|
|
|
||||||
|
|
@ -1,33 +1,32 @@
|
||||||
decl flow_control_test : Unit -> Unit
|
decl flow_control_test : Unit -> Unit
|
||||||
def flow_control_test = {
|
def flow_control_test = {
|
||||||
if ((a < b) || (a == b)) && (b < c) then IO.print: x
|
if (a < b ||. a == b) && (b < c) then IO.print: x
|
||||||
elif x < 0 then {
|
elif x < 0 then {
|
||||||
; ++x
|
; x += 1
|
||||||
; IO.print: y
|
; IO.print: y
|
||||||
} else {
|
} else {
|
||||||
return {}
|
return {}
|
||||||
}
|
}
|
||||||
|
|
||||||
while (a > 0) && (!array.is_empty:) do {
|
while (a > 0) && not: (array.is_empty:) do {
|
||||||
; --a
|
; a -= 1
|
||||||
; array.pop:
|
; array.pop:
|
||||||
}
|
}
|
||||||
|
|
||||||
while x < 10 do
|
while x < 10 do x +=. x + 3
|
||||||
x += x + 3
|
|
||||||
|
|
||||||
for i in 0..y do {
|
for i in 0--y do {
|
||||||
; IO.print: i
|
; IO.print: i
|
||||||
}
|
}
|
||||||
|
|
||||||
for & i & j in (& 0..y & 0..k) do {
|
for & i & j in & 0--y & 0--k do { // ??
|
||||||
; IO.print: 1
|
; IO.print: 1
|
||||||
; IO.print: 2
|
; IO.print: 2
|
||||||
; IO.print: 128
|
; IO.print: 128
|
||||||
}
|
}
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
; ++y
|
; y += 1
|
||||||
if y > 100 then
|
if y > 100 then
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@ def find_prefix_hashes : str = {
|
||||||
var hashes = (Array 'H).new: (str.size: + 1)
|
var hashes = (Array 'H).new: (str.size: + 1)
|
||||||
|
|
||||||
; hashes`0 = 'H.of: str`0
|
; hashes`0 = 'H.of: str`0
|
||||||
for i in 1..hashes.size: do {
|
for i in 1--hashes.size: do {
|
||||||
; hashes`i = hashes`(i - 1)
|
; hashes`i = hashes`(i - 1)
|
||||||
; hashes`i.append: str`i
|
; hashes`i.append: str`i
|
||||||
}
|
}
|
||||||
|
|
@ -37,8 +37,8 @@ def find_substring : str substr = {
|
||||||
const str_hashes = find_prefix_hashes Hash: str
|
const str_hashes = find_prefix_hashes Hash: str
|
||||||
const substr_hash = Hash.of: substr
|
const substr_hash = Hash.of: substr
|
||||||
|
|
||||||
for i in 0..(str_hashes.size: - substr.size:) do {
|
for i in 0--(str_hashes.size: - substr.size:) do {
|
||||||
const part_hash = Hash.diff: str_hashes`(i + substr->size:) str_hashes`i
|
const part_hash = Hash.diff: str_hashes`(i + substr.size:) str_hashes`i
|
||||||
|
|
||||||
if part_hash == substr_hash then {
|
if part_hash == substr_hash then {
|
||||||
; result.push: i
|
; result.push: i
|
||||||
|
|
|
||||||
259
tests/match.lang
259
tests/match.lang
|
|
@ -1,8 +1,12 @@
|
||||||
|
================================================================================
|
||||||
|
Match
|
||||||
|
================================================================================
|
||||||
|
|
||||||
decl fruit_cost : Fruit -> Int
|
decl fruit_cost : Fruit -> Int
|
||||||
def fruit_cost : fruit = {
|
def fruit_cost : fruit = {
|
||||||
return (match fruit with
|
return (match fruit with
|
||||||
| $Banana -> 11
|
| $Banana -> 11
|
||||||
| $Apple | $Orange -> 7)
|
| $Apple | $Orange -> 7)
|
||||||
}
|
}
|
||||||
|
|
||||||
decl amount_to_string : Int -> Bool -> String
|
decl amount_to_string : Int -> Bool -> String
|
||||||
|
|
@ -10,8 +14,253 @@ def amount_to_string : x is_zero_separated = {
|
||||||
const ans = match x with
|
const ans = match x with
|
||||||
| 0 ? is_zero_separated -> "Zero"
|
| 0 ? is_zero_separated -> "Zero"
|
||||||
| 0 | 1 | 2 | 3 | 4 -> "Few"
|
| 0 | 1 | 2 | 3 | 4 -> "Few"
|
||||||
| x ? (5..9).contains: x -> "Several"
|
| x ? (5--9).contains: x -> "Several"
|
||||||
| x ? (10..19).contains: x -> "Pack"
|
| x ? (10--19).contains: x -> "Pack"
|
||||||
| _ -> "Lots"
|
| _ -> "Lots"
|
||||||
return ans
|
return ans
|
||||||
}
|
}
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
(source_file
|
||||||
|
(source_statement
|
||||||
|
(namespace_statement
|
||||||
|
(function_declaration
|
||||||
|
(extended_name
|
||||||
|
(name_identifier))
|
||||||
|
(function_type
|
||||||
|
(scoped_any_type
|
||||||
|
(type_expression
|
||||||
|
(parametrized_type
|
||||||
|
(type_identifier))))
|
||||||
|
(scoped_any_type
|
||||||
|
(type_expression
|
||||||
|
(parametrized_type
|
||||||
|
(type_identifier))))))))
|
||||||
|
(source_statement
|
||||||
|
(namespace_statement
|
||||||
|
(function_definition_statement
|
||||||
|
(function_definition
|
||||||
|
(extended_name
|
||||||
|
(name_identifier))
|
||||||
|
(extended_name
|
||||||
|
(name_identifier)))
|
||||||
|
(superexpression
|
||||||
|
(expression
|
||||||
|
(prefixed_expression
|
||||||
|
(block
|
||||||
|
(block_statement
|
||||||
|
(prefixed_expression
|
||||||
|
(return_expression
|
||||||
|
(expression
|
||||||
|
(subexpression
|
||||||
|
(subexpression_token
|
||||||
|
(scoped_statement
|
||||||
|
(superexpression
|
||||||
|
(flow_control
|
||||||
|
(match
|
||||||
|
(expression
|
||||||
|
(subexpression
|
||||||
|
(subexpression_token
|
||||||
|
(name_expression
|
||||||
|
(extended_name
|
||||||
|
(name_identifier))))))
|
||||||
|
(match_case
|
||||||
|
(pattern
|
||||||
|
(type_constructor_pattern
|
||||||
|
(type_expression
|
||||||
|
(parametrized_type
|
||||||
|
(type_identifier)))))
|
||||||
|
(expression
|
||||||
|
(subexpression
|
||||||
|
(subexpression_token
|
||||||
|
(literal
|
||||||
|
(number_literal))))))
|
||||||
|
(match_case
|
||||||
|
(pattern
|
||||||
|
(type_constructor_pattern
|
||||||
|
(type_expression
|
||||||
|
(parametrized_type
|
||||||
|
(type_identifier))))))
|
||||||
|
(match_case
|
||||||
|
(pattern
|
||||||
|
(type_constructor_pattern
|
||||||
|
(type_expression
|
||||||
|
(parametrized_type
|
||||||
|
(type_identifier)))))
|
||||||
|
(expression
|
||||||
|
(subexpression
|
||||||
|
(subexpression_token
|
||||||
|
(literal
|
||||||
|
(number_literal)))))))))))))))))))))))
|
||||||
|
(source_statement
|
||||||
|
(namespace_statement
|
||||||
|
(function_declaration
|
||||||
|
(extended_name
|
||||||
|
(name_identifier))
|
||||||
|
(function_type
|
||||||
|
(scoped_any_type
|
||||||
|
(type_expression
|
||||||
|
(parametrized_type
|
||||||
|
(type_identifier))))
|
||||||
|
(scoped_any_type
|
||||||
|
(type_expression
|
||||||
|
(parametrized_type
|
||||||
|
(type_identifier))))
|
||||||
|
(scoped_any_type
|
||||||
|
(type_expression
|
||||||
|
(parametrized_type
|
||||||
|
(type_identifier))))))))
|
||||||
|
(source_statement
|
||||||
|
(namespace_statement
|
||||||
|
(function_definition_statement
|
||||||
|
(function_definition
|
||||||
|
(extended_name
|
||||||
|
(name_identifier))
|
||||||
|
(extended_name
|
||||||
|
(name_identifier))
|
||||||
|
(extended_name
|
||||||
|
(name_identifier)))
|
||||||
|
(superexpression
|
||||||
|
(expression
|
||||||
|
(prefixed_expression
|
||||||
|
(block
|
||||||
|
(block_statement
|
||||||
|
(variable_definition_statement
|
||||||
|
(any_name
|
||||||
|
(annotated_name
|
||||||
|
(extended_name
|
||||||
|
(name_identifier))))
|
||||||
|
(superexpression
|
||||||
|
(flow_control
|
||||||
|
(match
|
||||||
|
(expression
|
||||||
|
(subexpression
|
||||||
|
(subexpression_token
|
||||||
|
(name_expression
|
||||||
|
(extended_name
|
||||||
|
(name_identifier))))))
|
||||||
|
(match_case
|
||||||
|
(pattern
|
||||||
|
(literal
|
||||||
|
(number_literal)))
|
||||||
|
(expression
|
||||||
|
(subexpression
|
||||||
|
(subexpression_token
|
||||||
|
(name_expression
|
||||||
|
(extended_name
|
||||||
|
(name_identifier))))))
|
||||||
|
(expression
|
||||||
|
(subexpression
|
||||||
|
(subexpression_token
|
||||||
|
(literal
|
||||||
|
(string_literal))))))
|
||||||
|
(match_case
|
||||||
|
(pattern
|
||||||
|
(literal
|
||||||
|
(number_literal))))
|
||||||
|
(match_case
|
||||||
|
(pattern
|
||||||
|
(literal
|
||||||
|
(number_literal))))
|
||||||
|
(match_case
|
||||||
|
(pattern
|
||||||
|
(literal
|
||||||
|
(number_literal))))
|
||||||
|
(match_case
|
||||||
|
(pattern
|
||||||
|
(literal
|
||||||
|
(number_literal))))
|
||||||
|
(match_case
|
||||||
|
(pattern
|
||||||
|
(literal
|
||||||
|
(number_literal)))
|
||||||
|
(expression
|
||||||
|
(subexpression
|
||||||
|
(subexpression_token
|
||||||
|
(literal
|
||||||
|
(string_literal))))))
|
||||||
|
(match_case
|
||||||
|
(pattern
|
||||||
|
(extended_name
|
||||||
|
(name_identifier)))
|
||||||
|
(expression
|
||||||
|
(subexpression
|
||||||
|
(function_call_expression
|
||||||
|
(subexpression_token
|
||||||
|
(scoped_statement
|
||||||
|
(superexpression
|
||||||
|
(expression
|
||||||
|
(subexpression
|
||||||
|
(binary_operator_expression
|
||||||
|
(subexpression
|
||||||
|
(subexpression_token
|
||||||
|
(literal
|
||||||
|
(number_literal))))
|
||||||
|
(operator)
|
||||||
|
(subexpression
|
||||||
|
(subexpression_token
|
||||||
|
(literal
|
||||||
|
(number_literal))))))))))
|
||||||
|
(extended_name
|
||||||
|
(name_identifier))
|
||||||
|
(subexpression_token
|
||||||
|
(name_expression
|
||||||
|
(extended_name
|
||||||
|
(name_identifier)))))))
|
||||||
|
(expression
|
||||||
|
(subexpression
|
||||||
|
(subexpression_token
|
||||||
|
(literal
|
||||||
|
(string_literal))))))
|
||||||
|
(match_case
|
||||||
|
(pattern
|
||||||
|
(extended_name
|
||||||
|
(name_identifier)))
|
||||||
|
(expression
|
||||||
|
(subexpression
|
||||||
|
(function_call_expression
|
||||||
|
(subexpression_token
|
||||||
|
(scoped_statement
|
||||||
|
(superexpression
|
||||||
|
(expression
|
||||||
|
(subexpression
|
||||||
|
(binary_operator_expression
|
||||||
|
(subexpression
|
||||||
|
(subexpression_token
|
||||||
|
(literal
|
||||||
|
(number_literal))))
|
||||||
|
(operator)
|
||||||
|
(subexpression
|
||||||
|
(subexpression_token
|
||||||
|
(literal
|
||||||
|
(number_literal))))))))))
|
||||||
|
(extended_name
|
||||||
|
(name_identifier))
|
||||||
|
(subexpression_token
|
||||||
|
(name_expression
|
||||||
|
(extended_name
|
||||||
|
(name_identifier)))))))
|
||||||
|
(expression
|
||||||
|
(subexpression
|
||||||
|
(subexpression_token
|
||||||
|
(literal
|
||||||
|
(string_literal))))))
|
||||||
|
(match_case
|
||||||
|
(pattern
|
||||||
|
(extended_name
|
||||||
|
(name_identifier)))
|
||||||
|
(expression
|
||||||
|
(subexpression
|
||||||
|
(subexpression_token
|
||||||
|
(literal
|
||||||
|
(string_literal)))))))))))
|
||||||
|
(block_statement
|
||||||
|
(prefixed_expression
|
||||||
|
(return_expression
|
||||||
|
(expression
|
||||||
|
(subexpression
|
||||||
|
(subexpression_token
|
||||||
|
(name_expression
|
||||||
|
(extended_name
|
||||||
|
(name_identifier)))))))))))))))))
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ test All::Dev::Syntax::testing {
|
||||||
}
|
}
|
||||||
|
|
||||||
exec App::exe {
|
exec App::exe {
|
||||||
const b = 1117
|
const b = true
|
||||||
; do_something_different: b b
|
const c = false
|
||||||
|
; do_something_different: b b c
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,5 +4,5 @@ def test_tuples = {
|
||||||
const & t1 & t2 & t3 = f: x
|
const & t1 & t2 & t3 = f: x
|
||||||
|
|
||||||
; tuple1`0 = "b"
|
; tuple1`0 = "b"
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,10 +17,10 @@ decl ( < ) ('A : #Ord) : 'A -> 'A -> Bool
|
||||||
def ( < ) : a b = a.is_less_then: b
|
def ( < ) : a b = a.is_less_then: b
|
||||||
|
|
||||||
decl ( > ) ('A : #Ord) : 'A -> 'A -> Bool
|
decl ( > ) ('A : #Ord) : 'A -> 'A -> Bool
|
||||||
def ( > ) : a b = !(a <= b)
|
def ( > ) : a b = not: (a <= b)
|
||||||
|
|
||||||
decl ( <= ) ('A : #Ord) : 'A -> 'A -> Bool
|
decl ( <= ) ('A : #Ord) : 'A -> 'A -> Bool
|
||||||
def ( <= ) : a b = (a < b) || (a == b)
|
def ( <= ) : a b = a < b ||. a == b
|
||||||
|
|
||||||
decl ( >= ) ('A : #Ord) : 'A -> 'A -> Bool
|
decl ( >= ) ('A : #Ord) : 'A -> 'A -> Bool
|
||||||
def ( >= ) : a b = !(a < b)
|
def ( >= ) : a b = not: (a < b)
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,7 @@
|
||||||
|
================================================================================
|
||||||
|
Types
|
||||||
|
================================================================================
|
||||||
|
|
||||||
alias T1 = Int
|
alias T1 = Int
|
||||||
|
|
||||||
abstract (T2 : #A #B #C)
|
abstract (T2 : #A #B #C)
|
||||||
|
|
@ -6,3 +10,45 @@ abstract (T2 : #A #B #C)
|
||||||
let T2 = Int
|
let T2 = Int
|
||||||
let T2 = Float
|
let T2 = Float
|
||||||
let T2 = Complex
|
let T2 = Complex
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
(source_file
|
||||||
|
(source_statement
|
||||||
|
(namespace_statement
|
||||||
|
(alias_definition_statement
|
||||||
|
(type_identifier)
|
||||||
|
(type_expression
|
||||||
|
(parametrized_type
|
||||||
|
(type_identifier))))))
|
||||||
|
(source_statement
|
||||||
|
(abstract_type_definition_statement
|
||||||
|
(annotated_type
|
||||||
|
(type_identifier)
|
||||||
|
(parametrized_typeclass
|
||||||
|
(typeclass_identifier))
|
||||||
|
(parametrized_typeclass
|
||||||
|
(typeclass_identifier))
|
||||||
|
(parametrized_typeclass
|
||||||
|
(typeclass_identifier)))))
|
||||||
|
(source_statement
|
||||||
|
(namespace_statement
|
||||||
|
(alias_definition_statement
|
||||||
|
(type_identifier)
|
||||||
|
(type_expression
|
||||||
|
(parametrized_type
|
||||||
|
(type_identifier))))))
|
||||||
|
(source_statement
|
||||||
|
(namespace_statement
|
||||||
|
(alias_definition_statement
|
||||||
|
(type_identifier)
|
||||||
|
(type_expression
|
||||||
|
(parametrized_type
|
||||||
|
(type_identifier))))))
|
||||||
|
(source_statement
|
||||||
|
(namespace_statement
|
||||||
|
(alias_definition_statement
|
||||||
|
(type_identifier)
|
||||||
|
(type_expression
|
||||||
|
(parametrized_type
|
||||||
|
(type_identifier)))))))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue