Skip to content

Commit

Permalink
Merge pull request #1325 from GaijinEntertainment/version2parser_more
Browse files Browse the repository at this point in the history
more 0.6 syntax
  • Loading branch information
borisbat authored Oct 27, 2024
2 parents a7d7d09 + 7fa4500 commit 2392de5
Show file tree
Hide file tree
Showing 5 changed files with 44,812 additions and 44,634 deletions.
40 changes: 38 additions & 2 deletions examples/test/misc/hello_world.das
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ struct Baz : Bar { // structure with methods
}

class Cls { // class with methods
<comment> foobar : int; // note field annotation TOOD: IS THIS WHAT WE ARE DOING HERE?
@big // with metadata
@min=13
@max=42
private foobar : int;
static foobars : float;
def foo {
def public foo {
debug("foo");
}
def private private_method ( m : int ) { // todo: make it `private def static_method`
Expand Down Expand Up @@ -88,6 +91,39 @@ enum private Enum64 : uint64 { // 64 bit enum, private

// TODO: add example of enum with annotation. for now we don't have a builtin enum one

[export]
def enum_use_example {
let bf = MyEnum.ONE;
debug(bf);
}

///////////
// bitfield


bitfield EmptyBitfield {
}

bitfield MyBitfield {
ONE,
TWO,
THREE,
}

bitfield BitfieldWithTrailingComma {
ONE,
TWO,
}

[export]
def bitfield_use_example {
let bf = MyBitfield.ONE;
debug(bf);
}

///////
// main

[export]
def main {
var f <- {1=>"a", 2=>"b", 3=>"c", 4=>"d"};
Expand Down
25 changes: 25 additions & 0 deletions src/ast/ast_infer_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5150,15 +5150,40 @@ namespace das {
splitTypeName(var->name, moduleName, enumName);
getSearchModule(moduleName);
vector<Enumeration *> possibleEnums;
vector<TypeDecl *> possibleBitfields;
program->library.foreach([&](Module * mod) -> bool {
if ( auto possibleEnum = mod->findEnum(enumName) ) {
possibleEnums.push_back(possibleEnum.get());
}
if ( auto possibleBitfield = mod->findAlias(enumName) ) {
if ( possibleBitfield->isBitfield() ) {
possibleBitfields.push_back(possibleBitfield.get());
}
}
return true;
}, moduleName);
if ( possibleBitfields.size() && possibleEnums.size() ) {
error("ambiguous field lookup, '" + var->name + "' is both an enum and a bitfield", "", "",
expr->at, CompilationError::cant_get_field);
return Visitor::visit(expr);
}
if ( possibleEnums.size()==1 ) {
auto td = make_smart<TypeDecl>(possibleEnums.back());
return make_smart<ExprConstEnumeration>(expr->at, expr->name, td);
} else if ( possibleBitfields.size()==1 ) {
auto alias = possibleBitfields.back();
int bit = alias->findArgumentIndex(expr->name);
if ( bit!=-1 ) {
auto td = make_smart<TypeDecl>(*alias);
td->ref = false;
auto bitConst = new ExprConstBitfield(expr->at, 1u << bit);
bitConst->bitfieldType = make_smart<TypeDecl>(*alias);
return bitConst;
} else {
error("bitfield '" + expr->name + "' not found in " + describeType(alias), "", "",
expr->at, CompilationError::cant_get_field);
return Visitor::visit(expr);
}
}
}
if ( !expr->value->type || expr->value->type->isAliasOrExpr() ) return Visitor::visit(expr); // failed to infer
Expand Down
Loading

0 comments on commit 2392de5

Please sign in to comment.