type_check_visitor fixes, array access and function call syntax change

This commit is contained in:
ProgramSnail 2023-05-07 09:25:38 +03:00
parent f7080ba856
commit c1dec6a0d1
16 changed files with 426 additions and 311 deletions

View file

@ -124,6 +124,10 @@ private:
Context() = default;
bool DefineVariable(const std::string& name, utils::IdType type_id) {
if (name == "_") { // placeholder // TODO: check in all places
return true;
}
if (variables_.count(name) > 0) {
return false;
}

Binary file not shown.

View file

@ -985,7 +985,7 @@ void BuildVisitor::Visit(FunctionCallExpression* node) {
size_t child_count = parse_node.NamedChildCount();
if (child_count > excluded_child_count) { // always true (repeat1)
if (child_count > excluded_child_count) {
bool parameters_ended = false;
node->arguments.resize(child_count - excluded_child_count);

View file

@ -421,7 +421,7 @@ void PrintVisitor::Visit(ReferenceExpression* node) {
void PrintVisitor::Visit(AccessExpression* node) {
out_ << "[AccessExpression] (";
Visit(node->name.get());
out_ << ") : (";
out_ << ") ` (";
Visitor::Visit(node->id);
out_ << ')';
}
@ -447,15 +447,26 @@ void PrintVisitor::Visit(FunctionCallExpression* node) {
out_ << "] (";
bool is_first = true;
for (auto& parameter : node->parameters) {
if (!is_first) {
out_ << ", ";
}
is_first = false;
Visit(parameter.get());
out_ << ", ";
}
out_ << ") (";
out_ << ") : (";
is_first = true;
for (auto& argument : node->arguments) {
if (!is_first) {
out_ << ", ";
}
is_first = false;
Visitor::Visit(argument);
out_ << ", ";
}
out_ << ")";
}

View file

@ -524,12 +524,85 @@ void TypeCheckVisitor::Visit(LoopControlExpression& node) { // enum
// Operators
// TODO
void TypeCheckVisitor::Visit(BinaryOperatorExpression* node) {
// TODO: Check, that type is not abstract ??
auto maybe_operator_id = namespace_visitor_.FindFunction(std::nullopt, node->operator_name);
if (maybe_operator_id.has_value()) {
const info::definition::Function& operator_info = namespace_visitor_.GetGlobalInfo()->GetFunctionInfo(maybe_operator_id.value());
if (!operator_info.declaration.has_value()) {
error_handling::HandleTypecheckError("Operator declaration not found", node->base);
}
if (!operator_info.definition.has_value()) { // TODO: builtin, etc.
error_handling::HandleTypecheckError("Operator definition not found", node->base);
}
if (operator_info.argument_count != 3) { // 2 + return type
error_handling::HandleTypecheckError("Operator wrong argument count", node->base);
}
if (operator_info.declaration->parameters.size() > 0) {
error_handling::HandleTypecheckError("Operator with parameters", node->base);
}
Visitor::Visit(*operator_info.declaration.value().argument_types[0]);
utils::IdType left_expression_type = current_type_; // TODO: type in context of deduced types
Visitor::Visit(node->left_expression);
if (!context_manager_.AddTypeRequirement(current_type_, left_expression_type)) {
error_handling::HandleTypecheckError("Operator left expression has wrong type", node->base);
}
Visitor::Visit(*operator_info.declaration.value().argument_types[1]);
utils::IdType right_expression_type = current_type_; // TODO: type in context of deduced types
Visitor::Visit(node->right_expression);
if (!context_manager_.AddTypeRequirement(current_type_, right_expression_type)) {
error_handling::HandleTypecheckError("Operator right expression has wrong type", node->base);
}
Visitor::Visit(*operator_info.declaration.value().argument_types.back());
return;
}
Visitor::Visit(node->left_expression);
auto maybe_left_type = context_manager_.GetType<info::type::DefinedType>(current_type_);
utils::ValueType left_value_type = context_manager_.GetValueType(current_type_);
if (!maybe_left_type.has_value()) {
error_handling::HandleTypecheckError("Operator not found (type is not DefinedType)", node->base);
}
auto maybe_left_type_info = namespace_visitor_.GetGlobalInfo()->GetTypeInfo<info::definition::AnyType>(maybe_left_type.value()->GetTypeId());
std::string left_type_name = maybe_left_type_info.value().type.type;
auto maybe_const_operator_id = namespace_visitor_.FindMethod(std::nullopt,
left_type_name,
node->operator_name,
utils::IsConstModifier::Const);
if (left_value_type != utils::ValueType::Const) {
maybe_operator_id = namespace_visitor_.FindMethod(std::nullopt,
left_type_name,
node->operator_name,
utils::IsConstModifier::Var);
}
if (!maybe_operator_id.has_value() && !maybe_const_operator_id.has_value()) {
error_handling::HandleTypecheckError("Operator not found (method name not found)", node->base);
}
if (maybe_operator_id.has_value() && maybe_const_operator_id.has_value()) {
error_handling::HandleTypecheckError("Ambigious operator (const and var method)", node->base);
}
if (!maybe_operator_id.has_value()) {
error_handling::HandleTypecheckError("Operator not found", node->base);
maybe_operator_id = maybe_const_operator_id;
}
const info::definition::Function& operator_info = namespace_visitor_.GetGlobalInfo()->GetFunctionInfo(maybe_operator_id.value());
@ -542,7 +615,7 @@ void TypeCheckVisitor::Visit(BinaryOperatorExpression* node) {
error_handling::HandleTypecheckError("Operator definition not found", node->base);
}
if (operator_info.argument_count != 2) {
if (operator_info.argument_count != 2) { // argument + return type
error_handling::HandleTypecheckError("Operator wrong argument count", node->base);
}
@ -550,21 +623,15 @@ void TypeCheckVisitor::Visit(BinaryOperatorExpression* node) {
error_handling::HandleTypecheckError("Operator with parameters", node->base);
}
Visitor::Visit(*operator_info.declaration.value().argument_types[0]);
utils::IdType left_expression_type = current_type_; // TODO: type in context of deduced types
Visitor::Visit(node->left_expression);
if (!context_manager_.AddTypeRequirement(current_type_, left_expression_type)) {
error_handling::HandleTypecheckError("Operator left expression has wrong type", node->base);
}
Visitor::Visit(*operator_info.declaration.value().argument_types[1]);
Visitor::Visit(*operator_info.declaration.value().argument_types[9]);
utils::IdType right_expression_type = current_type_; // TODO: type in context of deduced types
Visitor::Visit(node->right_expression);
if (!context_manager_.AddTypeRequirement(current_type_, right_expression_type)) {
error_handling::HandleTypecheckError("Operator right expression has wrong type", node->base);
}
Visitor::Visit(*operator_info.declaration.value().argument_types.back());
}
void TypeCheckVisitor::Visit(UnaryOperatorExpression* node) {
@ -748,7 +815,7 @@ void TypeCheckVisitor::Visit(FunctionCallExpression* node) {
context_manager_.AddTypeRequirement(current_type_, argument_type);
}
Visitor::Visit(*function_declaration.argument_types.back()); // add return type to info ??
Visitor::Visit(*function_declaration.argument_types.back());
current_type_ = TypeInContext(current_type_, context);
}

View file

@ -1,21 +1,21 @@
decl test_arrays : Unit -> Unit
def test_arrays = {
var arr1 = ,1 ,2 ,3
const arr2 = Int._array 32
var arr3 = String._array 11
const arr2 = Int._array: 32
var arr3 = String._array: 11
const arr4 = 'a'..'z'
const n = 100
var @arr5 <- Int.@_new_array 10
var @arr5 <- Int.@_new_array: 10
var @arr6 <- String.@_new_array 10
var @arr6 <- String.@_new_array: 10
var ~arr6_reference = ~arr6
const elem1 = arr1:0
var elem2 = arr1:2
const ~ref1 = ~arr1:1
var ~ref2 = ~arr1:3
; arr1:1 = 123
const elem1 = arr1`0
var elem2 = arr1`2
const ~ref1 = ~arr1`1
var ~ref2 = ~arr1`3
; arr1`1 = 123
; ref1 = arr1:2 // set value
; ref1 = arr1`2 // set value
; ~ref1 = ~ref2 // set reference
}

View file

@ -1,29 +1,29 @@
decl flow_control_test : Unit -> Unit
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 {
; ++x
; IO.print y
; IO.print: y
} else {
return {}
}
while (a > 0) && (!array.is_empty ()) do {
while (a > 0) && (!array.is_empty:) do {
; --a
; array->pop
; array.pop:
}
while x < 10 do
x += x + 3
for i in 0..y do {
; IO.print i
; IO.print: i
}
for & i & j in (& 0..y & 0..k) do {
; IO.print 1
; IO.print 2
; IO.print 128
; IO.print: 1
; IO.print: 2
; IO.print: 128
}
loop {

View file

@ -5,24 +5,24 @@ decl fib : Int -> Int
def fib : n =
match n with
| 0 | 1 -> 1
| n ? n > 1 -> fib (n - 1) + fib n
| _ -> error "n must be positive"
| n ? n > 1 -> fib: (n - 1) + fib: n
| _ -> error: "n must be positive"
decl fact : Int -> Int
def fact : n =
match n with
| 0 -> 1
| n ? n > 0 -> n * fact (n - 1)
| _ -> error "n must be positive"
| n ? n > 0 -> n * fact: (n - 1)
| _ -> error: "n must be positive"
decl find_prefix_hashes ('H : (#AccHash Char)) : String -> (Array 'H)
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
for i in 1..hashes.size () do {
; hashes:i = hashes:(i - 1)
; hashes:i.append str:i
; hashes`0 = 'H.of: str`0
for i in 1..hashes.size: do {
; hashes`i = hashes`(i - 1)
; hashes`i.append: str`i
}
return hashes
@ -32,16 +32,16 @@ alias Hash = (AccHash Char)
decl find_substring : String -> String -> (Array Index)
def find_substring : str substr = {
var result = (Array Index).empty ()
var result = (Array Index).empty:
const str_hashes = find_prefix_hashes Hash str
const substr_hash = Hash.of substr
const str_hashes = find_prefix_hashes Hash: str
const substr_hash = Hash.of: substr
for i in 0..(str_hashes.size () - substr.size ()) do {
const part_hash = Hash.diff str_hashes(i + substr->size ()) str_hashes:i
for i in 0..(str_hashes.size: - substr.size:) do {
const part_hash = Hash.diff: str_hashes`(i + substr->size:) str_hashes`i
if part_hash == substr_hash then {
; result.push i
; result.push: i
}
}
@ -53,10 +53,10 @@ def is_empty = 0
decl do_something : Unit -> Unit
def do_something =
IO.print "Hello World!"
IO.print: "Hello World!"
decl mul : Int -> Int -> Int
def mul : x y = x * y
decl mul_10 : Int -> Int
def mul_10 = mul 10
def mul_10 = mul: 10

View file

@ -1,11 +1,11 @@
decl test_lambdas : Unit -> Unit
def test_lambdas = {
const lambda1 = \x -> x * x
const lambda2 = \x -> x.hash ()
const lambda2 = \x -> x.hash:
const lambda3 = \x y -> x + y
const lambda4 = \x -> {
; IO.print x
; IO.print: x
const y = x + x
return y
}

View file

@ -2,18 +2,20 @@
Match
================================================================================
decl fruit_cost : Fruit -> Int
def fruit_cost : fruit = {
return (match fruit with
| $Banana -> 11
| $Apple | $Orange -> 7)
}
decl amount_to_string : Int -> Bool -> String
def amount_to_string : x is_zero_separated = {
const ans = match x with
| 0 ? is_zero_separated () -> "Zero"
| 0 ? is_zero_separated -> "Zero"
| 0 | 1 | 2 | 3 | 4 -> "Few"
| x ? (5..9).contains x -> "Several"
| x ? (10..19).contains x -> "Pack"
| x ? (5..9).contains: x -> "Several"
| x ? (10..19).contains: x -> "Pack"
| _ -> "Lots"
return ans
}
@ -22,223 +24,247 @@ def amount_to_string : x is_zero_separated = {
(source_file
(source_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
(type_subexpression
(type_identifier)))))
(expression
(subexpression
(subexpression_token
(literal
(number_literal))))))
(match_case
(pattern
(type_constructor_pattern
(type_expression
(type_subexpression
(type_identifier))))))
(match_case
(pattern
(type_constructor_pattern
(type_expression
(type_subexpression
(type_identifier)))))
(expression
(subexpression
(subexpression_token
(literal
(number_literal))))))))))))))))))))))
(partition_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
(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
(partition_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
(partition_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
(partition_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))))))
(match_case
(pattern
(pattern_token
(literal
(number_literal))))
(expression
(subexpression
(function_call_expression
(extended_name
(name_identifier))
(function_argument
(subexpression_token
(literal
(unit_literal)))))))
(expression
(subexpression
(subexpression_token
(literal
(string_literal))))))
(match_case
(pattern
(pattern_token
(literal
(number_literal)))))
(match_case
(pattern
(pattern_token
(literal
(number_literal)))))
(match_case
(pattern
(pattern_token
(literal
(number_literal)))))
(match_case
(pattern
(pattern_token
(literal
(number_literal)))))
(match_case
(pattern
(pattern_token
(literal
(number_literal))))
(expression
(subexpression
(subexpression_token
(literal
(string_literal))))))
(match_case
(pattern
(pattern_token
(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))
(function_argument
(subexpression_token
(name_expression
(extended_name
(name_identifier))))))))
(expression
(subexpression
(subexpression_token
(literal
(string_literal))))))
(match_case
(pattern
(pattern_token
(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))
(function_argument
(subexpression_token
(name_expression
(extended_name
(name_identifier))))))))
(expression
(subexpression
(subexpression_token
(literal
(string_literal))))))
(match_case
(pattern
(pattern_token
(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))))))))))))))))
(name_identifier))))))))))))))))))

View file

@ -3,6 +3,6 @@ struct StructWithRef =
decl test_memory : Unit -> Unit
def test_memory = {
const @unique_ref1 <- Int.@_new 5
var @unique_ref2 <- Array.@of 1 2 3
const @unique_ref1 <- Int.@_new: 5
var @unique_ref2 <- Array.@of: 1 2 3
}

View file

@ -1,7 +1,8 @@
decl test_tuples : Unit -> Unit
def test_tuples = {
var tuple1 = & "a" & 2 & "hello"
const & t1 & t2 & t3 = f x
const & t1 & t2 & t3 = f: x
; tuple1:0 = "b"
; tuple1`0 = "b"
}

View file

@ -1,5 +1,5 @@
decl test_type_casting : Unit -> Unit
def test_type_casting = {
var x = y.as Int
var k = (f y x).as Float
var x = y.as Int:
var k = (f: y x).as Float:
}

View file

@ -11,10 +11,10 @@ typeclass E 'A =
& do_something : Unit -> 'A
decl ( == ) ('A : #Ord) : 'A -> 'A -> Bool
def ( == ) : a b = a.is_equal_to b
def ( == ) : a b = a.is_equal_to: b
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
def ( > ) : a b = !(a <= b)

View file

@ -15,39 +15,45 @@ let T2 = Complex
(source_file
(source_statement
(alias_definition_statement
(type_identifier)
(type_expression
(type_subexpression
(type_identifier)))))
(partition_statement
(namespace_statement
(alias_definition_statement
(type_identifier)
(type_expression
(parametrized_type
(type_identifier)))))))
(source_statement
(abstract_type_definition_statement
(annotated_type
(type_identifier)
(typeclass_expression
(typeclass_subexpression
(typeclass_identifier)))
(typeclass_expression
(typeclass_subexpression
(typeclass_identifier)))
(typeclass_expression
(typeclass_subexpression
(partition_statement
(abstract_type_definition_statement
(annotated_type
(type_identifier)
(parametrized_typeclass
(typeclass_identifier))
(parametrized_typeclass
(typeclass_identifier))
(parametrized_typeclass
(typeclass_identifier))))))
(source_statement
(alias_definition_statement
(type_identifier)
(type_expression
(type_subexpression
(type_identifier)))))
(partition_statement
(namespace_statement
(alias_definition_statement
(type_identifier)
(type_expression
(parametrized_type
(type_identifier)))))))
(source_statement
(alias_definition_statement
(type_identifier)
(type_expression
(type_subexpression
(type_identifier)))))
(partition_statement
(namespace_statement
(alias_definition_statement
(type_identifier)
(type_expression
(parametrized_type
(type_identifier)))))))
(source_statement
(alias_definition_statement
(type_identifier)
(type_expression
(type_subexpression
(type_identifier))))))
(partition_statement
(namespace_statement
(alias_definition_statement
(type_identifier)
(type_expression
(parametrized_type
(type_identifier))))))))

View file

@ -1,7 +1,7 @@
decl test_variants : Unit -> Unit
def test_variants = {
var variant1 = | 'a' | 2 | "hello"
var | val | err = f x
var | val | err = f: x
; val -?> "something" // open variant as value in expr