Skip to content

Commit

Permalink
Fix float to js conversion bug, add examples for string & array libs
Browse files Browse the repository at this point in the history
  • Loading branch information
GulgDev committed Jun 11, 2024
1 parent a8b8611 commit 7f99509
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 5 deletions.
2 changes: 2 additions & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
<option value="dom">JS & DOM</option>
<option value="ref">Ref</option>
<option value="callbacks">Callbacks</option>
<option value="string">String</option>
<option value="struct">Struct</option>
<option value="array">Array</option>
</select>
<script src="index.js"></script>
</body>
Expand Down
17 changes: 16 additions & 1 deletion src/demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ fn bar(callback) {
}
bar(fn("foo"));
`,
"string": `
use string;
str = string_format("Hello, %!", "world");
print str;
print string_length(str);
print string_replace(str, "world", "banana");
`,
"struct": `
use struct;
Expand All @@ -81,7 +89,14 @@ p2 = point_new(321, 123);
print "p1: " @ point_str(p1);
print "p2: " @ point_str(p2);
`
`,
"array": `
use array;
array = array(1, 2, 3, 4, 5);
print array_at(array, 0);
array_remove(array, 0);
print array_at(array, 0);`
};

const sigmaScript = new SigmaScript();
Expand Down
3 changes: 1 addition & 2 deletions src/sigmascript/lib/js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ export class JSLib extends NativeLib {
if (value === "unknown") return undefined;
if (value === "false") return false;
if (value === "true") return true;
const number = Number.parseFloat(value);
if (!Number.isNaN(number)) return number;
if (/^[0-9]+(\.[0-9]+)?$/.test(value)) return Number.parseFloat(value);
return value;
}

Expand Down
31 changes: 30 additions & 1 deletion src/sigmascript/lib/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,35 @@ export class StringLib extends NativeLib {
string_at: ([ string, index ]) => string.at(Number.parseInt(index)) ?? "unknown",
string_length: ([ string ]) => `${string.length}`,
string_slice: ([ string, start, end ]) => string.slice(Number.parseInt(start), Number.parseInt(end)),
string_replace: ([ string, search, replace ]) => string.replace(search, replace)
string_replace: ([ string, search, replace ]) => string.replaceAll(search, replace),
string_format: ([string, ...values]) => this.format(string, values)
};

format(string: string, values: string[]) {
if (values.length === 0)
return string;
let result = "";
const strlen = string.length;
let i = 0;
let j = 0;
while (i < strlen) {
const ch = string[i];
++i;
if (ch === "%") {
if (string[i] === "%")
++i;
else {
result += values[j];
++j;
if (j >= values.length) {
result += string.slice(i);
break;
}
continue;
}
}
result += ch;
}
return result;
}
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"outDir": "./dist/",
"noImplicitAny": true,
"module": "es6",
"target": "es2015",
"target": "es2021",
"jsx": "react",
"allowJs": true,
"moduleResolution": "node"
Expand Down

0 comments on commit 7f99509

Please sign in to comment.