Skip to content

Commit

Permalink
Fix ascii values for negative and overflowing numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
Lartu committed Jun 10, 2024
1 parent 939288e commit 74b2999
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/aux/aux_compile_line.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ void compile_line(vector<string> &tokens, compiler_state &state)
// Optimization for appending
state.add_code(get_c_variable(state, tokens[5]) + " += " + get_c_string(state, tokens[3]) + ";", state.where);
}else{
state.add_code(get_c_variable(state, tokens[5]) + " = " + get_c_string(state, tokens[1]) + " + " + get_c_string(state, tokens[3]) + ";", state.where);
state.add_code("join(" + get_c_string(state, tokens[1]) + ", " + get_c_string(state, tokens[3]) + ", " + get_c_string(state, tokens[5]) + ");", state.where);
}
return;
}
Expand Down Expand Up @@ -824,7 +824,7 @@ void compile_line(vector<string> &tokens, compiler_state &state)
badcode("GET ASCII CHARACTER statement outside PROCEDURE section",
state.where);
// C++ Code
state.add_code(get_c_variable(state, tokens[5]) + " = (char)(" +
state.add_code(get_c_variable(state, tokens[5]) + " = getAsciiChar(" +
get_c_expression(state, tokens[3]) + ");",
state.where);
return;
Expand Down
17 changes: 16 additions & 1 deletion src/ldpl_lib/ldpl_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ string graphemedText::operator[](size_t i)
cout << "Out-of-bounds index access." << endl;
exit(1);
}
return stringRep.substr(graphemeIndexMap[i], graphemeIndexMap[i+1] - graphemeIndexMap[i]);
return stringRep.substr(graphemeIndexMap[i], graphemeIndexMap[i + 1] - graphemeIndexMap[i]);
}
// [] for setting
/*string graphemedText::operator[](int i)
Expand Down Expand Up @@ -568,6 +568,7 @@ ofstream file_writing_stream;
string file_loading_line;
ldpl_number VAR_ERRORCODE = 0;
graphemedText VAR_ERRORTEXT = "";
graphemedText joinvar = ""; // Generic temporary use text variable (used by join but can be used by any other statement as well)

// Forward declarations
graphemedText to_ldpl_string(ldpl_number x);
Expand Down Expand Up @@ -758,6 +759,20 @@ ldpl_number get_char_num(graphemedText chr)
return ord;
}

graphemedText getAsciiChar(ldpl_number value)
{
if (value < 0)
return "?";
if (value > 127)
return "?";
return (char)value;
}

void join(const graphemedText &a, const graphemedText &b, graphemedText &c)
{
c = a + b;
}

graphemedText charat(graphemedText &s, ldpl_number pos)
{
size_t _pos = floor(pos);
Expand Down

0 comments on commit 74b2999

Please sign in to comment.