mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-05 22:48:42 +00:00
changes for new grammar, fixes
This commit is contained in:
parent
3106a64949
commit
f912cdac41
10 changed files with 59 additions and 5 deletions
|
|
@ -307,6 +307,7 @@ struct VariableDefinitionStatement {
|
|||
utils::AssignmentModifier assignment_modifier;
|
||||
AnyName name;
|
||||
SuperExpression value;
|
||||
std::optional<SuperExpression> in_expression;
|
||||
};
|
||||
|
||||
struct FunctionDeclaration {
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
Subproject commit 24bcf6e960957daf5662b764480f1d56637b1712
|
||||
Subproject commit ab391d2d8d5251422abc94b80a51438faddb19c7
|
||||
|
|
@ -167,6 +167,14 @@ void BuildVisitor::Visit(VariableDefinitionStatement* node) {
|
|||
// error
|
||||
}
|
||||
|
||||
size_t child_count = parse_node.NamedChildCount();
|
||||
|
||||
if (child_count > 2) { // name, value [, in_expression]
|
||||
current_node_ = parse_node.ChildByFieldName("in_expression");
|
||||
node->in_expression.emplace();
|
||||
Visit(node->in_expression.value());
|
||||
}
|
||||
|
||||
current_node_ = parse_node;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -47,11 +47,22 @@ void ExecuteVisitor::Visit(AliasDefinitionStatement* node) {
|
|||
}
|
||||
|
||||
void ExecuteVisitor::Visit(VariableDefinitionStatement* node) {
|
||||
// TODO: optional variable definitions
|
||||
|
||||
if (node->in_expression.has_value()) {
|
||||
context_manager_.EnterContext();
|
||||
}
|
||||
|
||||
Visitor::Visit(node->value);
|
||||
|
||||
is_const_definition_ = node->modifier;
|
||||
Visitor::Visit(node->name); // current_type_ passed from value
|
||||
is_const_definition_ = std::nullopt;
|
||||
|
||||
if (node->in_expression.has_value()) {
|
||||
Visitor::Visit(node->in_expression.value());
|
||||
context_manager_.ExitContext();
|
||||
}
|
||||
}
|
||||
|
||||
void ExecuteVisitor::Visit(FunctionDeclaration* node) {
|
||||
|
|
|
|||
|
|
@ -93,6 +93,12 @@ void PrintVisitor::Visit(VariableDefinitionStatement* node) {
|
|||
Visitor::Visit(node->name);
|
||||
out_ << "] = (";
|
||||
Visitor::Visit(node->value);
|
||||
|
||||
if (node->in_expression.has_value()) {
|
||||
out_ << ") in (";
|
||||
Visitor::Visit(node->in_expression.value());
|
||||
}
|
||||
|
||||
out_ << ")\n";
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -101,7 +101,11 @@ void TypeCheckVisitor::Visit(AliasDefinitionStatement* node) {
|
|||
|
||||
// TODO: move, etc.
|
||||
void TypeCheckVisitor::Visit(VariableDefinitionStatement* node) {
|
||||
is_in_statement_ = true;
|
||||
// TODO: optional variable definitions
|
||||
|
||||
if (node->in_expression.has_value()) {
|
||||
context_manager_.EnterContext();
|
||||
}
|
||||
|
||||
Visitor::Visit(node->value);
|
||||
// current_type from value automatically passed to name definitions
|
||||
|
|
@ -110,8 +114,12 @@ void TypeCheckVisitor::Visit(VariableDefinitionStatement* node) {
|
|||
Visitor::Visit(node->name);
|
||||
is_const_definition_ = std::nullopt;
|
||||
|
||||
if (node->in_expression.has_value()) {
|
||||
Visitor::Visit(node->in_expression.value());
|
||||
context_manager_.ExitContext();
|
||||
}
|
||||
|
||||
current_type_ = internal_to_abstract_type_.at(info::type::InternalType::Unit);
|
||||
is_in_statement_ = false;
|
||||
|
||||
node->base.type_ = current_type_;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -123,6 +123,12 @@ void TypedPrintVisitor::Visit(VariableDefinitionStatement* node) {
|
|||
Visitor::Visit(node->name);
|
||||
out_ << "] = (";
|
||||
Visitor::Visit(node->value);
|
||||
|
||||
if (node->in_expression.has_value()) {
|
||||
out_ << ") in (";
|
||||
Visitor::Visit(node->in_expression.value());
|
||||
}
|
||||
|
||||
out_ << ")\n";
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -328,6 +328,9 @@ void Visitor::Visit(AliasDefinitionStatement* node) {
|
|||
void Visitor::Visit(VariableDefinitionStatement* node) {
|
||||
Visit(node->name);
|
||||
Visit(node->value);
|
||||
if (node->in_expression.has_value()) {
|
||||
Visit(node->in_expression.value());
|
||||
}
|
||||
}
|
||||
|
||||
void Visitor::Visit(FunctionDeclaration* node) {
|
||||
|
|
|
|||
|
|
@ -106,10 +106,10 @@ namespace var \div {
|
|||
|
||||
typeclass \eq =
|
||||
& var ( == ) : \eq -> \bool
|
||||
& var ( != ) : \eq -> \bool
|
||||
& var ( <> ) : \eq -> \bool
|
||||
|
||||
namespace var \eq {
|
||||
def ( != ) : x = not: (self == x)
|
||||
def ( <> ) : x = not: (self == x)
|
||||
}
|
||||
|
||||
//
|
||||
|
|
|
|||
11
tests/variables.lang
Normal file
11
tests/variables.lang
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
decl test-variables : \int -> \unit
|
||||
def test-variables : a = {
|
||||
var x = if a < 123 then "aaa" else "bbb"
|
||||
|
||||
var y = 543.32 in do-something:
|
||||
|
||||
var z = 543.32 in {
|
||||
for x in 1--10 do print: 111
|
||||
if scan:[int] < 11 then do-something-another: z "aaa"
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue