Skip to content

Commit 009cf91

Browse files
authored
Merge pull request #14 from smuuf/strict
Handling of strings and numbers is now strict
2 parents 2bf3004 + 17f48f9 commit 009cf91

29 files changed

+108
-90
lines changed

docs/language_reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Example usage:
3333
a = true;
3434
b = 1 == 2; // false
3535
c = b == false; // true
36-
d = c == "hello"; // ERR: Cannot compare: 'bool' and 'string' @ code: c == "hello"
36+
d = c == "hello"; // ERR: Cannot compare 'bool' and 'string'
3737
e = "hello" == r"[0-9]"; // false
3838
f = "hello" == r"l{2}"; // true
3939
```

example/04.primi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
a = 4 / (2 - 1) * 100;
22
damn = 153 * 2;
33

4-
c = "zaplaťte pouze " + a + " Kč za skvělou " + damn;
4+
c = "zaplaťte pouze " + to_string(a) + " Kč za skvělou " + damn.to_string();
55
g = 1;
66

77
while (damn > 10) {

example/08.primi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function sub(x, y) {
99
}
1010

1111
a = 1 + add(sub(4, 3), 3);
12-
a + "\n";
12+
a.to_string() + "\n";
1313

1414
function nested(a) {
1515
return add(a, sub(2,3));

example/09.primi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ function sub(x, y) {
6262

6363
}
6464

65-
a = 1 + add(sub(4, 3), 3);
65+
a = (1 + add(sub(4, 3), 3)).to_string();
6666
a = a + "\n";
6767

6868
a = 4 / (2 - 1) * 100;
6969
damn = 153 * 2;
70-
c = "zaplaťte pouze " + a + " Kč za skvělou " + damn;
70+
c = "zaplaťte pouze " + a.to_string() + " Kč za skvělou " + to_string(damn);
7171

7272
g = 1;
7373
d = 2;
@@ -86,7 +86,7 @@ while (damn < 10000) {
8686
");
8787

8888
damn = damn * d - 1 * d;
89-
c = g + "0";
89+
c = g + to_number("0");
9090

9191
if (c - 4 * 2) {
9292
if (4 + (123 * 7) / 8) {

example/bench_all.primi

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

22
a = 1 + 2;
3-
b = "1" + 2;
4-
c = 1 + "2";
5-
d = "x" + 1;
6-
e = 1 + "x";
3+
b = to_number("1") + 2;
4+
c = to_string(1) + "2";
5+
d = "x".to_number() + 1;
6+
e = to_string(1) + "x";
77
f = "x" + "1";
88
g = "1" + "x";
99
h = "abc" + " def";
@@ -91,8 +91,8 @@ d = 1 * "2";
9191
e = 1 * "2.4";
9292
f = 8 / 4;
9393
g = 4 / 16;
94-
h = 123 / a;
95-
i = 123 / b;
94+
h = 123 / to_number(a);
95+
i = 123 / to_number(b);
9696
j = (1 + (3 / (4 - 5)) + 2 / (37 * 2 / 8 - 42));
9797
k = 1 + 2 * 3 / 4 - 5 / 6 * 7 + 8;
9898
a = "abcdefg";
@@ -141,7 +141,7 @@ _definitions = [
141141
];
142142
f = "rádoby čau".string_replace(_definitions);
143143
g = "vole".string_replace("v", "l").string_replace("o", "i") + "k";
144-
h = ("číslo je " + 000.cos()).string_replace(["č": "c", "í": "i"]);
144+
h = ("číslo je " + 000.cos().to_string()).string_replace(["č": "c", "í": "i"]);
145145
i = "kokot je " + "oo".string_replace("o", "e");
146146
a_1 = 9.sqrt();
147147
b_1 = 3.pow();

src/Repl.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use \Smuuf\Primi\Helpers\Common;
99
use \Smuuf\Primi\Colors;
1010
use \Smuuf\Primi\Interpreter;
11+
use \Smuuf\Primi\IContext;
1112
use \Smuuf\Primi\IReadlineDriver;
1213

1314
class Repl extends \Smuuf\Primi\StrictObject {
@@ -56,12 +57,19 @@ public function start() {
5657
private function loop() {
5758

5859
$i = $this->interpreter;
60+
$c = $i->getContext();
61+
5962
readline_completion_function(function() { return []; });
63+
6064
while (true) {
6165

6266
$input = $this->gatherLines();
6367

6468
switch (trim($input)) {
69+
case '?':
70+
$this->printContext($c);
71+
continue 2;
72+
break;
6573
case '':
6674
// Ignore (skip) empty input.
6775
continue 2;
@@ -91,7 +99,7 @@ private function loop() {
9199

92100
}
93101

94-
public function printResult(Value $result = null): void {
102+
private function printResult(Value $result = null): void {
95103

96104
if ($result === null) {
97105
return;
@@ -100,12 +108,12 @@ public function printResult(Value $result = null): void {
100108
printf(
101109
"%s %s\n",
102110
$result->getStringValue(),
103-
!$this->rawOutput ? $this->formatType($result) : null
111+
!$this->rawOutput ? self::formatType($result) : null
104112
);
105113

106114
}
107115

108-
private function formatType(Value $value) {
116+
private static function formatType(Value $value) {
109117

110118
return Colors::get(sprintf(
111119
"{darkgrey}(%s %s){_}",
@@ -115,6 +123,15 @@ private function formatType(Value $value) {
115123

116124
}
117125

126+
private function printContext(IContext $c): void {
127+
128+
foreach ($c->getVariables() as $name => $value) {
129+
echo "$name: ";
130+
$this->printResult($value);
131+
}
132+
133+
}
134+
118135
private function gatherLines(): string {
119136

120137
$gathering = false;

src/extensions/psl/CastingExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static function to_string(Value $value): StringValue {
2929
public static function to_regex(Value $value): RegexValue {
3030

3131
// Allow regexes to be casted to regex.
32-
if ($value instanceof Regex) {
32+
if ($value instanceof RegexValue) {
3333
return $value;
3434
}
3535

src/handlers/Addition.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ public static function handle(array $node, Context $context) {
2626
} catch (InternalBinaryOperationxception $e) {
2727

2828
throw new ErrorException(sprintf(
29-
"Cannot %s types '%s' and '%s'",
30-
$e->getOperator() === "+" ? "add" : "subtract",
29+
"Cannot use operator '%s' with '%s' and '%s'",
30+
$e->getOperator(),
3131
($e->getLeft())::TYPE,
3232
($e->getRight())::TYPE
3333
), $node);

src/handlers/Comparison.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static function handle(array $node, Context $context) {
3737
} catch (\TypeError $e) {
3838

3939
throw new ErrorException(sprintf(
40-
"Cannot compare types '%s' and '%s'",
40+
"Cannot compare '%s' and '%s'",
4141
$leftReturn::TYPE,
4242
$rightReturn::TYPE
4343
), $node);

src/handlers/ForeachStatement.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static function handle(array $node, Context $context) {
2222

2323
if (!$subject instanceof ISupportsIteration) {
2424
throw new \Smuuf\Primi\ErrorException(
25-
"Cannot iterate over '{$node['left']['text']}' variable",
25+
sprintf("Cannot iterate over '%s'", $subject::TYPE),
2626
$node
2727
);
2828
}

0 commit comments

Comments
 (0)