Skip to content

Commit

Permalink
unicode
Browse files Browse the repository at this point in the history
  • Loading branch information
tmaradc committed Jun 15, 2023
1 parent f55781d commit 17694fc
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 26 deletions.
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 || (55295 < i && i < 57344)) {
let i = getInt(instr, state.env, 0);
if(i > 1114111 || i < 0) {
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 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 does not correspond to a valid character.
- `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 56193;
i: int = const 1114112;

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 56193 cannot be converted to char
error: value 1114112 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

0 comments on commit 17694fc

Please sign in to comment.