Skip to content

Commit db2612e

Browse files
committed
Fixed semantics for issue #44, code gen needs to be changed!
1 parent 34ce9bc commit db2612e

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

compiler/ast.hpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,15 @@ class LValue : public Expr
857857
{
858858
if (stringLiteral)
859859
{
860-
this->type = TYPE_BYTE_ARRAY;
860+
if(!indices.empty()) {
861+
if(indices.size() > 1) {
862+
semantic_error(line, column, "String literal can only be indexed once.");
863+
}
864+
indices[0]->check_type(TYPE_INT);
865+
this->type = TYPE_BYTE;
866+
} else {
867+
this->type = TYPE_BYTE_ARRAY;
868+
}
861869
return;
862870
}
863871

@@ -935,11 +943,21 @@ class LValue : public Expr
935943

936944
gvar->setAlignment(llvm::Align(1));
937945

938-
// Pointer to first character
939946
llvm::Constant *zero = AST::c32(0);
940-
std::vector<llvm::Constant *> idxList = {zero, zero};
947+
948+
llvm::Value* charIdx = zero;
949+
950+
/* TODO: Fix this for issue #44
951+
if(!indices.empty()) {
952+
llvm::Value *v = indices[0]->igen();
953+
if (!v->getType()->isIntegerTy(32))
954+
v = AST::Builder.CreateZExtOrTrunc(v, AST::i32, "idx");
955+
charIdx = v;
956+
}
957+
*/
958+
941959
llvm::Constant *strPtr = llvm::ConstantExpr::getGetElementPtr(
942-
strConst->getType(), gvar, idxList);
960+
strConst->getType(), gvar, {zero, charIdx});
943961

944962
return strPtr;
945963
}

testing/issues/weird_string.dana

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def main
2+
var c is byte
3+
var i is int
4+
5+
i := 3
6+
c := "gazonk"[i]
7+
8+
writeChar: c
9+
writeChar: '\n'

0 commit comments

Comments
 (0)