Skip to content

Commit

Permalink
Added negative numbers and bit literals
Browse files Browse the repository at this point in the history
  • Loading branch information
Ariel Simulevski committed Feb 7, 2019
1 parent 034d582 commit d20c036
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 12 deletions.
10 changes: 10 additions & 0 deletions examples/numbersBits.psph
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
dci32 -21
dcf -132.4

cbase
dcb false
dcb true
ldbc 1
ldbc 0
xor
syscall 0x01
98 changes: 98 additions & 0 deletions src/bytecoderuntime/bytecoderuntime.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
package bytecoderuntime

import (
"../compiler"
"../datatypes"
"encoding/binary"
"math"
)

func getUint64FromBytes(a, b, c, d, e, f, g, h byte) uint64 {
return (uint64(a) << 56) + (uint64(b) << 48) + (uint64(c) << 40) + (uint64(d) << 32) + (uint64(e) << 24) + (uint64(f) << 16) + (uint64(g) << 8) + uint64(h)
}

func getUint32FromBytes(a, b, c, d byte) uint32 {
return (uint32(a) << 24) + (uint32(b) << 16) + (uint32(c) << 8) + uint32(d)
}

func getUint16FromBytes(a, b byte) uint16 {
return (uint16(a) << 8) + uint16(b)
}
Expand Down Expand Up @@ -38,7 +49,94 @@ func Run(bytes []byte) int8 {
code := bytes[labelPtr:]

for e := 0; e < len(code); e++ {
opcode := getUint16FromBytes(code[e],code[e + 1])
e += 2

parameter := false

switch opcode {

default:
//Opcode has a parameter
parameter = true
}

if parameter {
paramType := code[e]
e++

param := datatypes.Data{}

switch paramType {
case compiler.Int:
intSize := code[e]
e++

switch intSize {
case compiler.Int8:
param.Type = datatypes.Int8
param.Value = int8(code[e])
e++

case compiler.Int16:
param.Type = datatypes.Int16
param.Value = int16(getUint16FromBytes(code[e], code[e + 1]))
e += 2

case compiler.Int32:
param.Type = datatypes.Int32
param.Value = int32(getUint32FromBytes(code[e], code[e + 1], code[e + 2], code[e + 3]))
e += 4

case compiler.Int64:
param.Type = datatypes.Int64
param.Value = int64(getUint64FromBytes(code[e], code[e + 1], code[e + 2], code[e + 3], code[e + 4], code[e + 5], code[e + 6], code[e + 7]))
e += 8
}

case compiler.Float:
floatSize := code[e]
e++

switch floatSize {
case compiler.Float32:
bytes := []byte{code[e], code[e + 1], code[e + 2], code[e + 3]}
e += 4

bits := binary.LittleEndian.Uint32(bytes)

param.Type = datatypes.Float32
param.Value = math.Float32frombits(bits)

case compiler.Float64:
bytes := []byte{code[e], code[e + 1], code[e + 2], code[e + 3], code[e + 4], code[e + 5], code[e + 6], code[e + 7]}
e += 8

bits := binary.LittleEndian.Uint64(bytes)

param.Type = datatypes.Float64
param.Value = math.Float64frombits(bits)
}
case compiler.StringA:
case compiler.StringU:
case compiler.Bit:
param.Type = datatypes.Bit
val := code[e]
e++

if val == 0x0 {
param.Value = false
}else {
param.Value = true
}
case compiler.Ptr:
case compiler.Label:
param.Type = datatypes.Label
param.Value = getUint64FromBytes(code[e], code[e + 1], code[e + 2], code[e + 3], code[e + 4], code[e + 5], code[e + 6], code[e + 7])
e+= 8
case compiler.Variable:
}
}
}

return 0
Expand Down
23 changes: 17 additions & 6 deletions src/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,15 @@ func Compile(root types.Root, outname string) int {
write(getUint64Bytes(labels[root.Commands[e].Param.Text]))
} else {
switch root.Commands[e].Param.Kind {
case types.Bit:
write([]byte{Bit})
switch root.Commands[e].Param.Text {
case "true":
write([]byte{0x1})
case "false":
write([]byte{0x0})
}

case types.Name:
write([]byte{Variable})

Expand Down Expand Up @@ -288,20 +297,22 @@ func Compile(root types.Root, outname string) int {
var num float64
num, _ = strconv.ParseFloat(root.Commands[e].Param.Text, 64)

var buf [8]byte
binary.BigEndian.PutUint64(buf[:], math.Float64bits(num))
bits := math.Float64bits(num)
bytes := make([]byte,8)
binary.LittleEndian.PutUint64(bytes,bits)

write(buf[:])
write(bytes[:])
default: //if size isn't stated, use 32 bit
write([]byte{Float32})

var num float64
num, _ = strconv.ParseFloat(root.Commands[e].Param.Text, 32)

var buf [4]byte
binary.BigEndian.PutUint32(buf[:], math.Float32bits(float32(num)))
bits := math.Float32bits(float32(num))
bytes := make([]byte,4)
binary.LittleEndian.PutUint32(bytes,bits)

write(buf[:])
write(bytes[:])
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/datatypes/datatypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type DataType int16

// Enum for Persephone variable types
const (
Label DataType = 0x0
Bit DataType = 0x01
Ptr DataType = 0x02

Expand Down
10 changes: 9 additions & 1 deletion src/lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,15 @@ func Lex(lines []string) (tokens []types.Token) {
current.Kind = types.Label
discard()
}
} else if isNumber.MatchString(string(letter())) {

if current.Text == "true" || current.Text == "false"{
current.Kind = types.Bit
}
} else if isNumber.MatchString(string(letter())) || (letter() == rune('-') && isNumber.MatchString(string(code[index + 1]))){
if letter() == rune('-') {
consume()
}

consume()

if letter() == rune('x') {
Expand Down
2 changes: 1 addition & 1 deletion src/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func parseStatement() (command types.Command) {
command.Command = tokens[index]
index++

if tokens[index].Kind == types.Name || tokens[index].Kind == types.Number || tokens[index].Kind == types.HexNumber || tokens[index].Kind == types.Float || tokens[index].Kind == types.String || tokens[index].Kind == types.Pointer {
if tokens[index].Kind == types.Name || tokens[index].Kind == types.Number || tokens[index].Kind == types.HexNumber || tokens[index].Kind == types.Float || tokens[index].Kind == types.String || tokens[index].Kind == types.Pointer || tokens[index].Kind == types.Bit{
command.Param = tokens[index]
return
}
Expand Down
7 changes: 3 additions & 4 deletions src/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,12 +297,11 @@ func bitOp(s stack, op types.Op) stack {
return s
}

s, left = s.Pop()
if left.Type != datatypes.Bit {
panic("Value is not of type bit!")
}

s, left = s.Pop()

var result bool

switch op {
Expand Down Expand Up @@ -372,9 +371,9 @@ func declareBitConstant(command types.Command, c []datatypes.Data) []datatypes.D
val := false

switch command.Param.Text {
case "0":
case "false":
val = false
case "1":
case "true":
val = true
default:
panic("Value is not of type bit!")
Expand Down
1 change: 1 addition & 0 deletions src/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const (
Pointer Kind = "pointer"
Label Kind = "label"
Unknown Kind = "unknown"
Bit Kind = "bit"
)

type Token struct {
Expand Down

0 comments on commit d20c036

Please sign in to comment.