Skip to content

Commit

Permalink
codegen: Fix some tests
Browse files Browse the repository at this point in the history
Type cast primitive types
  • Loading branch information
delasy committed Oct 14, 2024
1 parent 191ded9 commit bdedc4f
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 141 deletions.
23 changes: 22 additions & 1 deletion packages/codegen/src/codegen
Original file line number Diff line number Diff line change
Expand Up @@ -1567,7 +1567,20 @@ export obj Codegen {
}

expression := item.asAs()
return self._generateExpression(ref expression.expression)

if !(item.contextExtra is Analyzer.AsExpressionContextExtra) {
throw error_NewError("Expected AsExpression context extra")
}

initialType := Analyzer.contextInitial(item)
contextExtra := item.contextExtra as Analyzer.AsExpressionContextExtra
result := self._generateExpression(ref expression.expression)

if Analyzer.match(contextExtra.fromType, initialType) {
return result
}

return self._wrap(result, item, contextExtra.fromType, initialType)
}

fn _generateAssignmentExpression (mut self: ref Codegen, item: ref Parser.Expression) AST.CExpression? {
Expand Down Expand Up @@ -3230,6 +3243,14 @@ export obj Codegen {
AST.createPropertyAccessExpression(expression, "data"),
"v" + typeDefIdx.str(),
)
} elif !Analyzer.match(t, targetType) && (
targetType.name == "bool" ||
targetType.name == "byte" ||
targetType.name == "char" ||
targetType.name == "rune" ||
targetType.isNumber()
) {
expression = AST.createCastExpression(AST.createType(self._type(targetType)), expression)
}

if !transform {
Expand Down
66 changes: 30 additions & 36 deletions packages/codegen/test/codegen/as-expression-root.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,12 @@ main {
c as f64;

d: byte = 65;
d as i8;
d as u8;
d as char;
d as int;
d as float;

e := 'a';
e as i8;
e as u8;
e as int;
e as float;

Expand All @@ -52,42 +49,39 @@ int main (void) {
unsigned char d_0;
wchar_t e_0;
bool f_0;
int8_t a_0 = 10;
a_0;
a_0;
a_0;
a_0;
a_0;
a_0;
a_0;
b_0 = 10;
b_0;
b_0;
b_0;
b_0;
b_0;
b_0;
int8_t a_0 = (int8_t) 10;
(int16_t) a_0;
(int32_t) a_0;
(int32_t) a_0;
(int64_t) a_0;
(double) a_0;
(float) a_0;
(double) a_0;
b_0 = (uint8_t) 10;
(uint16_t) b_0;
(uint32_t) b_0;
(uint64_t) b_0;
(double) b_0;
(float) b_0;
(double) b_0;
c_0 = 10;
c_0;
c_0;
c_0;
c_0;
c_0;
(int32_t) c_0;
(int64_t) c_0;
(double) c_0;
(float) c_0;
(double) c_0;
d_0 = 65;
d_0;
d_0;
d_0;
d_0;
d_0;
(uint8_t) d_0;
(char) d_0;
(int32_t) d_0;
(double) d_0;
e_0 = L'a';
e_0;
e_0;
e_0;
e_0;
(int32_t) e_0;
(double) e_0;
f_0 = true;
f_0;
f_0;
f_0;
f_0;
(int8_t) f_0;
(uint8_t) f_0;
(int32_t) f_0;
(double) f_0;
}
===== output =====
Loading

0 comments on commit bdedc4f

Please sign in to comment.