Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StringLiteral #4

Merged
merged 23 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ npm run mtsc ./tests/singleVar.ts
- [ ] Make semicolon a statement ender, not statement separator.
- Hint: You'll need a predicate to peek at the next token and decide if it's the start of an element.
- Bonus: Switch from semicolon to newline as statement ender.
- [ ] Add string literals.
- [x] Add string literals.
- [ ] Add let.
- Then add use-before-declaration errors in the checker.
- Finally, add an ES2015 -> ES5 transform that transforms `let` to `var`.
Expand Down
4 changes: 4 additions & 0 deletions baselines/reference/singleTypedVar.errors.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@
{
"pos": 17,
"message": "Cannot assign initialiser of type 'number' to variable with declared type 'string'."
},
{
"pos": 41,
"message": "Cannot assign initialiser of type 'string' to variable with declared type 'number'."
}
]
2 changes: 1 addition & 1 deletion baselines/reference/singleTypedVar.js.baseline
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"var s = 1"
"var s = 1;\nvar n = 'test';\n"
15 changes: 15 additions & 0 deletions baselines/reference/singleTypedVar.tree.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@
"kind": "Literal",
"value": 1
}
},
{
"kind": "Var",
"name": {
"kind": "Identifier",
"text": "n"
},
"typename": {
"kind": "Identifier",
"text": "number"
},
"init": {
"kind": "Literal",
"value": "'test'"
}
}
]
}
1 change: 1 addition & 0 deletions baselines/reference/stringLIteral.errors.baseline
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
1 change: 1 addition & 0 deletions baselines/reference/stringLIteral.js.baseline
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"var singleQuote = 'singleQuote';\nvar doubleQuote = \"doubleQuote\""
43 changes: 43 additions & 0 deletions baselines/reference/stringLIteral.tree.baseline
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"locals": {
"singleQuote": [
{
"kind": "Var",
"pos": 3
}
],
"doubleQuote": [
{
"kind": "Var",
"pos": 36
}
]
},
"statements": [
{
"kind": "Var",
"name": {
"kind": "Identifier",
"text": "singleQuote"
},
"init": {
"kind": "Literal",
"value": "'singleQuote'"
}
},
{
"kind": "Var",
"name": {
"kind": "Identifier",
"text": "doubleQuote"
},
"init": {
"kind": "Literal",
"value": "\"doubleQuote\""
}
},
{
"kind": "EmptyStatement"
}
]
}
2 changes: 1 addition & 1 deletion src/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export function check(module: Module) {
error(expression.pos, 'Could not resolve ' + expression.text);
return errorType;
case Node.Literal:
return numberType;
return typeof expression.value === 'string' ? stringType : numberType;
case Node.Assignment:
const v = checkExpression(expression.value);
const t = checkExpression(expression.name);
Expand Down
4 changes: 4 additions & 0 deletions src/lex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ export function lex(s: string): Lexer {
text in keywords
? keywords[text as keyof typeof keywords]
: Token.Identifier;
} else if (['"', "'"].includes(s.charAt(pos))) {
scanForward((c) => /[_a-zA-Z0-9'"]/.test(c));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strings should be able to contain any character except the matching " or '. And you'll need to allow for escaping like with \' as well.

text = s.slice(start, pos);
token = Token.String;
} else {
pos++;
switch (s.charAt(pos - 1)) {
Expand Down
2 changes: 2 additions & 0 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export function parse(lexer: Lexer): Module {
return { kind: Node.Identifier, text: lexer.text(), pos };
} else if (tryParseToken(Token.Literal)) {
return { kind: Node.Literal, value: +lexer.text(), pos };
} else if (tryParseToken(Token.String)) {
return { kind: Node.Literal, value: lexer.text(), pos };
}
error(
pos,
Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export enum Token {
Semicolon = 'Semicolon',
Colon = 'Colon',
Whitespace = 'Whitespace',
String = 'String',
Unknown = 'Unknown',
BOF = 'BOF',
EOF = 'EOF',
Expand Down Expand Up @@ -49,7 +50,7 @@ export type Identifier = Location & {

export type Literal = Location & {
kind: Node.Literal;
value: number;
value: number | string;
};

export type Assignment = Location & {
Expand Down
1 change: 1 addition & 0 deletions tests/singleTypedVar.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
var s: string = 1;
var n: number = 'test';
2 changes: 2 additions & 0 deletions tests/stringLiteral.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var singleQuote = 'singleQuote';
var doubleQuote = "doubleQuote";