Skip to content

Commit

Permalink
implement js strings
Browse files Browse the repository at this point in the history
  • Loading branch information
serkonda7 committed Dec 13, 2023
1 parent 40ed3c2 commit 855ce8a
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ _unreleased_
- Use of unknown struct field in infix
- Assign from unknown function or method

### Language Interoperability
- _[JS]_ Implement native strings `js'my str'`

### Pointers
- Add pointer dereferencing: `^my_ptr`
- Fix `typeof()` for pointers
Expand Down
1 change: 1 addition & 0 deletions lib/bait/ast/ast.bt
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ pub:

pub struct StringLiteral{
pub:
lang Language
val string
pos token.Pos
}
Expand Down
13 changes: 10 additions & 3 deletions lib/bait/gen/js/expr.bt
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,16 @@ fun (mut g Gen) selector_expr(node ast.SelectorExpr) {

fun (mut g Gen) string_literal(node ast.StringLiteral) {
val := util.escape_char(util.escape_linebreak(node.val), `\"`)
g.write('from_js_string("')
g.write(val)
g.write('")')

if node.lang == .js {
g.write('"')
g.write(val)
g.write('"')
} else {
g.write('from_js_string("')
g.write(val)
g.write('")')
}
}

fun (mut g Gen) string_inter_literal(node ast.StringInterLiteral) {
Expand Down
13 changes: 10 additions & 3 deletions lib/bait/parser/expr.bt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fun (mut p Parser) single_expr() ast.Expr {
return p.number_literal()
}
.string {
return p.string_literal()
return p.string_literal(.bait)
}
.key_fun {
return p.anon_fun()
Expand Down Expand Up @@ -241,7 +241,7 @@ fun (mut p Parser) hash_expr()ast.Expr{
return p.name_expr(lang)
}

str_node := p.string_literal() as ast.StringLiteral
str_node := p.string_literal(.bait) as ast.StringLiteral
return ast.HashExpr{
lang = lang
val = str_node.val
Expand Down Expand Up @@ -340,6 +340,12 @@ fun (mut p Parser) name_expr(lang ast.Language) ast.Expr{
if p.next_tok.kind == .dot and p.tok.val[0].is_capital() and not p.tok.val.is_upper() {
return p.enum_val(true)
}

if p.next_tok.kind == .string and p.tok.val == 'js' {
p.next()
return p.string_literal(.js)
}

return p.ident(lang)
}

Expand Down Expand Up @@ -387,11 +393,12 @@ fun (mut p Parser) prefix_expr()ast.PrefixExpr{
}
}

fun (mut p Parser) string_literal()ast.Expr{
fun (mut p Parser) string_literal(lang ast.Language)ast.Expr{
pos := p.tok.pos
if p.next_tok.kind != .dollar {
p.next()
return ast.StringLiteral{
lang = lang
val = p.prev_tok.val
pos = pos
}
Expand Down
File renamed without changes.
7 changes: 7 additions & 0 deletions tests/interop/js/string_test.js.bt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-FileCopyrightText: 2023-present Lukas Neubert <[email protected]>
// SPDX-License-Identifier: MPL-2.0

fun test_js_string() {
s := js'hello'
assert s == #JS.'"hello"' as string
}

0 comments on commit 855ce8a

Please sign in to comment.