Skip to content

Commit

Permalink
Merge pull request #255 from tmaradc/improvements-char
Browse files Browse the repository at this point in the history
Char issues
  • Loading branch information
sampsyo authored Jun 29, 2023
2 parents 9bb9c86 + ec0f81b commit c87d7cd
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 27 deletions.
3 changes: 3 additions & 0 deletions bril-txt/briltxt.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,9 @@ def type_to_str(type):

def value_to_str(type, value):
if not isinstance(type, dict) and type.lower() == "char":
control_chars_reverse = {y: x for x, y in control_chars.items()}
if ord(value) in control_chars_reverse:
value = control_chars_reverse[ord(value)]
return "'{}'".format(value)
else:
return str(value).lower()
Expand Down
16 changes: 8 additions & 8 deletions brili.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ function evalInstr(instr: bril.Instruction, state: State): Action {
else
value = BigInt(Math.floor(instr.value))
} else if (typeof instr.value === "string") {
if(instr.value.length > 1) throw error(`char must have one character`);
if([...instr.value].length !== 1) throw error(`char must have one character`);
value = instr.value;
} else {
value = instr.value;
Expand Down Expand Up @@ -754,18 +754,18 @@ function evalInstr(instr: bril.Instruction, state: State): Action {
}

case "char2int": {
let code = getChar(instr, state.env, 0).charCodeAt(0);
let val = BigInt.asIntN(64, BigInt(code));
let code = getChar(instr, state.env, 0).codePointAt(0);
let val = BigInt.asIntN(64, BigInt(code as number));
state.env.set(instr.dest, val);
return NEXT;
}

case "int2char": {
let i = getInt(instr, state.env, 0)
if(i > 65535 || i < 0) {
let i = getInt(instr, state.env, 0);
if (i > 1114111 || i < 0 || (55295 < i && i < 57344)) {
throw error(`value ${i} cannot be converted to char`);
}
let val = String.fromCharCode(Number(i));
}
let val = String.fromCodePoint(Number(i));
state.env.set(instr.dest, val);
return NEXT;
}
Expand Down Expand Up @@ -854,7 +854,7 @@ function evalFunc(func: bril.Function, state: State): Value | null {

function parseChar(s: string): string {
let c = s;
if (c.length == 1) {
if ([...c].length === 1) {
return c;
} else {
throw error(`char argument to main must have one character; got ${s}`);
Expand Down
8 changes: 4 additions & 4 deletions docs/lang/char.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ The character extension adds one new base type:

"char"

Characters are [UTF-16][] code units.
Characters are a singular [Unicode character][].


[UTF-16]: https://en.wikipedia.org/wiki/UTF-16
[Unicode character]: https://www.unicode.org/glossary/#character

Operations
----------
Expand All @@ -26,8 +26,8 @@ Comparison operators, which take two `char` values and produce a `bool`:

Conversion operators:

- `char2int`: One argument: a variable of type `char`. Returns an integer between 0 and 65535 representing the UTF-16 code unit of the given value.
- `int2char`: One argument: a variable of type `int`. Returns the corresponding UTF-16 code unit. Throws if the value is not between 0 and 65535, inclusive.
- `char2int`: One argument: a variable of type `char`. Returns an integer representing the Unicode code point of the given value.
- `int2char`: One argument: a variable of type `int`. Returns the corresponding Unicode character. Throws if the value does not correspond to a valid Unicode code point.

Printing
--------
Expand Down
4 changes: 2 additions & 2 deletions test/interp-error/char-error/badconversion.bril
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@main {
i: int = const 95000;
i: int = const 56193;

c: char = int2char i;

print i;
print c;
}
2 changes: 1 addition & 1 deletion test/interp-error/char-error/badconversion.err
Original file line number Diff line number Diff line change
@@ -1 +1 @@
error: value 95000 cannot be converted to char
error: value 56193 cannot be converted to char
3 changes: 2 additions & 1 deletion test/interp/char/char.bril
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
c1: char = const 'h';
c2: char = const 'e';
c3: char = const 'y';
print c1 c2 c3;
c4: char = const '🐶';
print c1 c2 c3 c4;
}
2 changes: 1 addition & 1 deletion test/interp/char/char.out
Original file line number Diff line number Diff line change
@@ -1 +1 @@
h e y
h e y 🐶
6 changes: 3 additions & 3 deletions test/interp/char/char_args.bril
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# ARGS: h e y
@main(char_one : char, char_two : char, char_three : char) {
print char_one char_two char_three;
# ARGS: h e y 🐱
@main(c1: char, c2: char, c3: char, c4: char) {
print c1 c2 c3 c4;
}
2 changes: 1 addition & 1 deletion test/interp/char/char_args.out
Original file line number Diff line number Diff line change
@@ -1 +1 @@
h e y
h e y 🐱
12 changes: 8 additions & 4 deletions test/interp/char/conversions.bril
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
@main() {
c1: char = const 'A';
c2: char = const '𝛀';
i1: int = const 65;

c2: char = int2char i1;
i2: int = char2int c1;
i2: int = const 120512;

print c2 i2;
c3: char = int2char i1;
i3: int = char2int c1;
c4: char = int2char i2;
i4: int = char2int c2;

print c3 i3 c4 i4;
}
2 changes: 1 addition & 1 deletion test/interp/char/conversions.out
Original file line number Diff line number Diff line change
@@ -1 +1 @@
A 65
A 65 𝛀 120512
1 change: 1 addition & 0 deletions test/print/char.bril
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@main {
c1: char = const '7';
c2: char = const 'a';
c2: char = const '\n';
}
3 changes: 2 additions & 1 deletion test/print/char.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"name": "main",
"instrs": [
{ "op": "const", "type": "char", "dest": "c1", "value": "7" },
{ "op": "const", "type": "char", "dest": "c2", "value": "a" }
{ "op": "const", "type": "char", "dest": "c2", "value": "a" },
{ "op": "const", "type": "char", "dest": "c2", "value": "\n" }
],
"args": []
}
Expand Down

0 comments on commit c87d7cd

Please sign in to comment.