Added escape sequences

This commit is contained in:
Eduard Urbach 2024-07-31 17:50:31 +02:00
parent cdbfa744b7
commit c8824e699a
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
12 changed files with 87 additions and 7 deletions

View File

@ -114,6 +114,7 @@ This is what generates expressions from tokens.
- [x] Branches - [x] Branches
- [x] Loops - [x] Loops
- [x] Hexadecimal, octal and binary literals - [x] Hexadecimal, octal and binary literals
- [x] Escape sequences
- [ ] Data structures - [ ] Data structures
- [ ] Type system - [ ] Type system
- [ ] Type operator: `|` (`User | Error`) - [ ] Type operator: `|` (`User | Error`)

View File

@ -1,7 +1,7 @@
import sys import sys
main() { main() {
print("Hello", 5) print("Hello\n", 6)
} }
print(address, length) { print(address, length) {

View File

@ -132,6 +132,12 @@ func (a Assembler) Finalize() ([]byte, []byte) {
case LABEL: case LABEL:
labels[x.Data.(*Label).Name] = Address(len(code)) labels[x.Data.(*Label).Name] = Address(len(code))
case LOAD:
switch operands := x.Data.(type) {
case *MemoryRegister:
code = x64.LoadRegister(code, operands.Register, operands.Address.Offset, operands.Address.Length, operands.Address.Base)
}
case MODULO: case MODULO:
code = modulo(code, x.Data) code = modulo(code, x.Data)

View File

@ -6,6 +6,7 @@ import (
"git.akyoto.dev/cli/q/src/build/cpu" "git.akyoto.dev/cli/q/src/build/cpu"
"git.akyoto.dev/cli/q/src/build/errors" "git.akyoto.dev/cli/q/src/build/errors"
"git.akyoto.dev/cli/q/src/build/expression" "git.akyoto.dev/cli/q/src/build/expression"
"git.akyoto.dev/cli/q/src/build/token"
) )
// ExpressionToRegister puts the result of an expression into the specified register. // ExpressionToRegister puts the result of an expression into the specified register.
@ -29,6 +30,13 @@ func (f *Function) ExpressionToRegister(node *expression.Expression, register cp
return err return err
} }
if node.Token.Kind == token.Array {
array := f.VariableByName(node.Children[0].Token.Text(f.File.Bytes))
offset, err := f.Number(node.Children[1].Token)
f.MemoryRegister(asm.LOAD, asm.Memory{Base: array.Register, Offset: byte(offset), Length: 1}, register)
return err
}
if len(node.Children) == 1 { if len(node.Children) == 1 {
if !node.Token.IsUnaryOperator() { if !node.Token.IsUnaryOperator() {
return errors.New(errors.MissingOperand, f.File, node.Token.End()) return errors.New(errors.MissingOperand, f.File, node.Token.End())

View File

@ -35,7 +35,7 @@ func (f *Function) Number(t token.Token) (int, error) {
case token.Rune: case token.Rune:
r := t.Bytes(f.File.Bytes) r := t.Bytes(f.File.Bytes)
r = r[1 : len(r)-1] r = String(r)
if len(r) == 0 { if len(r) == 0 {
return 0, errors.New(errors.InvalidRune, f.File, t.Position+1) return 0, errors.New(errors.InvalidRune, f.File, t.Position+1)

43
src/build/core/String.go Normal file
View File

@ -0,0 +1,43 @@
package core
import "bytes"
// String replaces the escape sequences in the contents of a string token with the respective characters.
func String(data []byte) []byte {
data = data[1 : len(data)-1]
escape := bytes.IndexByte(data, '\\')
if escape == -1 {
return data
}
tmp := make([]byte, 0, len(data))
for {
tmp = append(tmp, data[:escape]...)
switch data[escape+1] {
case '0':
tmp = append(tmp, '\000')
case 't':
tmp = append(tmp, '\t')
case 'n':
tmp = append(tmp, '\n')
case 'r':
tmp = append(tmp, '\r')
case '"':
tmp = append(tmp, '"')
case '\'':
tmp = append(tmp, '\'')
case '\\':
tmp = append(tmp, '\\')
}
data = data[escape+2:]
escape = bytes.IndexByte(data, '\\')
if escape == -1 {
return tmp
}
}
}

View File

@ -34,7 +34,8 @@ func (f *Function) TokenToRegister(t token.Token, register cpu.Register) error {
return nil return nil
case token.String: case token.String:
data := t.Bytes(f.File.Bytes)[1 : t.Length-1] data := t.Bytes(f.File.Bytes)
data = String(data)
label := f.AddBytes(data) label := f.AddBytes(data)
f.RegisterLabel(asm.MOVE, register, label) f.RegisterLabel(asm.MOVE, register, label)
return nil return nil

View File

@ -76,7 +76,7 @@ func Tokenize(buffer []byte) List {
i++ i++
for i < Position(len(buffer)) { for i < Position(len(buffer)) {
if buffer[i] == limiter { if buffer[i] == limiter && (buffer[i-1] != '\\' || buffer[i-2] == '\\') {
end = i + 1 end = i + 1
i++ i++
break break

View File

@ -15,7 +15,7 @@ var examples = []struct {
Output string Output string
ExitCode int ExitCode int
}{ }{
{"hello", "", "Hello", 0}, {"hello", "", "Hello\n", 0},
{"factorial", "", "120", 0}, {"factorial", "", "120", 0},
{"fibonacci", "", "55", 0}, {"fibonacci", "", "55", 0},
{"gcd", "", "21", 0}, {"gcd", "", "21", 0},

View File

@ -0,0 +1,9 @@
main() {
assert '\0' == 0
assert '\t' == 9
assert '\n' == 10
assert '\r' == 13
assert '\"' == 34
assert '\'' == 39
assert '\\' == 92
}

View File

@ -0,0 +1,10 @@
main() {
str := "\0\t\n\r\"\'\\"
assert str[0] == '\0'
assert str[1] == '\t'
assert str[2] == '\n'
assert str[3] == '\r'
assert str[4] == '\"'
assert str[5] == '\''
assert str[6] == '\\'
}

View File

@ -24,11 +24,13 @@ var programs = []struct {
{"reassign", "", "", 0}, {"reassign", "", "", 0},
{"reuse", "", "", 0}, {"reuse", "", "", 0},
{"return", "", "", 0}, {"return", "", "", 0},
{"math", "", "", 0},
{"precedence", "", "", 0},
{"binary", "", "", 0}, {"binary", "", "", 0},
{"octal", "", "", 0}, {"octal", "", "", 0},
{"hexadecimal", "", "", 0}, {"hexadecimal", "", "", 0},
{"math", "", "", 0}, {"escape-rune", "", "", 0},
{"precedence", "", "", 0}, {"escape-string", "", "", 0},
{"bitwise-and", "", "", 0}, {"bitwise-and", "", "", 0},
{"bitwise-or", "", "", 0}, {"bitwise-or", "", "", 0},
{"bitwise-xor", "", "", 0}, {"bitwise-xor", "", "", 0},