Skip to content

Commit

Permalink
bitfield declarations are now supported via dot
Browse files Browse the repository at this point in the history
bitfield syntax in 0.6 is now supported via comma and with the trailing comma
  • Loading branch information
borisbat committed Oct 27, 2024
1 parent a94d1ac commit 7fa4500
Show file tree
Hide file tree
Showing 5 changed files with 17,436 additions and 17,334 deletions.
37 changes: 35 additions & 2 deletions examples/test/misc/hello_world.das
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ class Cls { // class with methods
@big // with metadata
@min=13
@max=42
foobar : int;
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 @@ -91,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 7fa4500

Please sign in to comment.