minor fixes and toInt.kyn

This commit is contained in:
2025-10-06 20:29:36 +11:00
parent 26cb591c49
commit e4432cbe21
6 changed files with 62 additions and 11 deletions

View File

@@ -51,7 +51,7 @@ Value handleStringGet(const Value& subject, const std::vector<Value>& args) {
error("String index out of bounds"); error("String index out of bounds");
return Value(); return Value();
} }
return Value(std::string(1, subject.string_val.at(index))); return Value("\"" + std::string(1, subject.string_val.at(index)) + "\"");
} }
void handleStringSet(std::string var_name, Value& subject, const Value& index_val, const Value& new_val) { void handleStringSet(std::string var_name, Value& subject, const Value& index_val, const Value& new_val) {

View File

@@ -77,18 +77,18 @@ Instruction::Instruction(std::vector<Value> toks) {
Value::Value(std::string stringval) { Value::Value(std::string stringval) {
// This constructor will attempt to parse the string into the most specific type possible. // This constructor will attempt to parse the string into the most specific type possible.
if (stringval.length() > 2 && stringval.front() == '<' && stringval.back() == '>') {
valtype = ValueType::TypePlaceholder;
type_placeholder_name = stringval.substr(1, stringval.length() - 2);
return;
}
if (stringval.length() >= 2 && stringval.front() == '"' && stringval.back() == '"') { if (stringval.length() >= 2 && stringval.front() == '"' && stringval.back() == '"') {
valtype = ValueType::String; valtype = ValueType::String;
string_val = interpretEscapeSequences(stringval.substr(1, stringval.length() - 2)); string_val = interpretEscapeSequences(stringval.substr(1, stringval.length() - 2));
return; return;
} }
if (stringval.length() > 2 && stringval.front() == '<' && stringval.back() == '>') {
valtype = ValueType::TypePlaceholder;
type_placeholder_name = stringval.substr(1, stringval.length() - 2);
return;
}
if (stringval.empty()) { if (stringval.empty()) {
valtype = ValueType::None; valtype = ValueType::None;
string_val = ""; string_val = "";
@@ -274,7 +274,7 @@ std::vector<Value> split(std::string line) {
splitvals.push_back(Value(buf)); splitvals.push_back(Value(buf));
} }
buf = ""; buf = "";
} else if (chr == '(' || chr == '[') { } else if ((chr == '(' || chr == '[') && !instring) {
if (brackets == 0) { if (brackets == 0) {
bracket_type = chr; bracket_type = chr;
if (!buf.empty()) { if (!buf.empty()) {
@@ -285,7 +285,7 @@ std::vector<Value> split(std::string line) {
buf += chr; buf += chr;
} }
brackets++; brackets++;
} else if (chr == ')' || chr == ']') { } else if ((chr == ')' || chr == ']') && !instring) {
brackets--; brackets--;
if (brackets == 0) { if (brackets == 0) {
if (!buf.empty()) { if (!buf.empty()) {

View File

@@ -67,6 +67,7 @@ struct Instruction {
InstructionType instruction = InstructionType::None; InstructionType instruction = InstructionType::None;
std::vector<Value> args; std::vector<Value> args;
std::string toString() const; std::string toString() const;
int lineNum;
Instruction() = default; Instruction() = default;
Instruction(std::vector<std::string> toks); Instruction(std::vector<std::string> toks);
Instruction(std::vector<Value> toks); Instruction(std::vector<Value> toks);

View File

@@ -7,6 +7,7 @@ Value modules::concat(std::vector<Value> args) {
std::string buf; std::string buf;
for (Value val : args) { for (Value val : args) {
switch (val.valtype) { switch (val.valtype) {
case ValueType::Identifier:
case ValueType::String: case ValueType::String:
buf += val.string_val; buf += val.string_val;
break; break;
@@ -17,7 +18,7 @@ Value modules::concat(std::vector<Value> args) {
buf += std::to_string(val.double_val); buf += std::to_string(val.double_val);
break; break;
default: default:
error("Can only concatenate String, Int, and Double values"); error("Can only concatenate String, Int, and Double values. Value given: " + val.toString());
break; break;
} }
} }

View File

@@ -9,8 +9,10 @@ std::vector<Instruction> parse(std::string program) {
std::vector<std::string> lines; std::vector<std::string> lines;
std::string buf; std::string buf;
int blockTracker = 0; int blockTracker = 0;
int lineNum = 0;
for (char chr : program) { for (char chr : program) {
if (chr == '\n') lineNum ++;
if (chr == '{') { if (chr == '{') {
blockTracker++; blockTracker++;
} else if (chr == '}') { } else if (chr == '}') {
@@ -23,7 +25,7 @@ std::vector<Instruction> parse(std::string program) {
if ((chr == '\n' || chr == ';') && blockTracker == 0) { if ((chr == '\n' || chr == ';') && blockTracker == 0) {
if (!buf.empty()) { if (!buf.empty()) {
lines.push_back(buf); lines.push_back(trim(buf));
buf.clear(); buf.clear();
} }
} else { } else {

47
tests/toInt.kyn Normal file
View File

@@ -0,0 +1,47 @@
fun toInt in {
assert $in is <String>
let retint = 0
let counter = 0
let power = ($in size)
let size = ($in size)
while compare $counter < $size {
let char = ($in $counter)
power = (math $power - 1)
if compare $char == "1" {
retint = (math $retint + (math 1 * 10 ^ $power))
}
if compare $char == "2" {
retint = (math $retint + (math 2 * 10 ^ $power))
}
if compare $char == "3" {
retint = (math $retint + (math 3 * 10 ^ $power))
}
if compare $char == "4" {
retint = (math $retint + (math 4 * 10 ^ $power))
}
if compare $char == "5" {
retint = (math $retint + (math 5 * 10 ^ $power))
}
if compare $char == "6" {
retint = (math $retint + (math 6 * 10 ^ $power))
}
if compare $char == "7" {
retint = (math $retint + (math 7 * 10 ^ $power))
}
if compare $char == "8" {
retint = (math $retint + (math 8 * 10 ^ $power))
}
if compare $char == "9" {
retint = (math $retint + (math 9 * 10 ^ $power))
}
if compare $char == "0" {
# whole lotta nothing
}
counter = (math $counter + 1)
}
return $retint
}
let myInt = (toInt "4738927643289")
println $myInt