Skip to content

Commit

Permalink
change error type definition
Browse files Browse the repository at this point in the history
  • Loading branch information
douyixuan committed Aug 22, 2024
1 parent d4e2e0c commit be82334
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 237 deletions.
1 change: 1 addition & 0 deletions compiler/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func Parse(input []lexer.Item, options *option.Options) *FileNode {
"uintptr": {},
"byte": {},
"string": {},
"error": {},
},
}

Expand Down
22 changes: 18 additions & 4 deletions pkg/builtin/builtin.cell
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
package builtin

type error interface {
Error() string
IsNone() bool
NotNone() bool
type error table {
none bool
message string
}

func (e *error) Error() string {
return e.message
}

func (e *error) IsNone() bool {
return e.none
}

func (e *error) NotNone() bool {
return !e.none
}

type Type byte
extern func copy(dst []Type, src []Type) int32
25 changes: 5 additions & 20 deletions pkg/errors/errors.cell
Original file line number Diff line number Diff line change
@@ -1,32 +1,17 @@
package errors

type errorData table {
none bool
s string
}

func (e *errorData) Error() string {
return e.s
}

func (e *errorData) IsNone() bool {
return e.none
}

func (e *errorData) NotNone() bool {
return !e.none
const NONE = error {
none: true
}

func New(text string) error {
var e errorData
e.s = text
var e error
e.message = text
return e
}

func None() error {
var e errorData
e.none = true
return e
return NONE
}

var ErrUnsupported = New("unsupported operation")
214 changes: 1 addition & 213 deletions pkg/math/bits/bits.cell
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package bits implements bit counting and manipulation
// functions for the predeclared unsigned integer types.
//
// Functions in this package may be implemented directly by
// the compiler, for better performance. For those functions
// the code in this package will not be used. Which
// functions are implemented by the compiler depends on the
// architecture and the Go release.
package bits


Expand Down Expand Up @@ -214,17 +202,7 @@ func TrailingZeros64(x uint64) int64 {
if x == 0 {
return 64
}
// If popcount is fast, replace code below with return popcount(^x & (x - 1)).
//
// x & -x leaves only the right-most bit set in the word. Let k be the
// index of that bit. Since only a single bit is set, the value is two
// to the power of k. Multiplying by a power of two is equivalent to
// left shifting, in this case by k bits. The de Bruijn (64 bit) constant
// is such that all six bit, consecutive substrings are distinct.
// Therefore, if we have a left shifted version of this constant we can
// find by how many bits it was shifted by looking at which six bit
// substring ended up at the top of the word.
// (Knuth, volume 4, section 7.3.1)

return int64(deBruijn64tab[(x&-x)*deBruijn64>>(64-6)])
}

Expand All @@ -233,193 +211,3 @@ func TrailingZeros(x uint64) int64 {
return TrailingZeros64(x)
}

// // // --- OnesCount ---

// // const m0 = 0x5555555555555555 // 01010101 ...
// // const m1 = 0x3333333333333333 // 00110011 ...
// // const m2 = 0x0f0f0f0f0f0f0f0f // 00001111 ...
// // const m3 = 0x00ff00ff00ff00ff // etc.
// // const m4 = 0x0000ffff0000ffff

// // // OnesCount returns the number of one bits ("population count") in x.
// // func OnesCount(x uint) int {
// // if UintSize == 32 {
// // return OnesCount32(uint32(x))
// // }
// // return OnesCount64(uint64(x))
// // }

// // // OnesCount8 returns the number of one bits ("population count") in x.
// // func OnesCount8(x uint8) int {
// // return int(pop8tab[x])
// // }

// // // OnesCount16 returns the number of one bits ("population count") in x.
// // func OnesCount16(x uint16) int {
// // return int(pop8tab[x>>8] + pop8tab[x&0xff])
// // }

// // // OnesCount32 returns the number of one bits ("population count") in x.
// // func OnesCount32(x uint32) int {
// // return int(pop8tab[x>>24] + pop8tab[x>>16&0xff] + pop8tab[x>>8&0xff] + pop8tab[x&0xff])
// // }

// // // OnesCount64 returns the number of one bits ("population count") in x.
// // func OnesCount64(x uint64) int {
// // // Implementation: Parallel summing of adjacent bits.
// // // See "Hacker's Delight", Chap. 5: Counting Bits.
// // // The following pattern shows the general approach:
// // //
// // // x = x>>1&(m0&m) + x&(m0&m)
// // // x = x>>2&(m1&m) + x&(m1&m)
// // // x = x>>4&(m2&m) + x&(m2&m)
// // // x = x>>8&(m3&m) + x&(m3&m)
// // // x = x>>16&(m4&m) + x&(m4&m)
// // // x = x>>32&(m5&m) + x&(m5&m)
// // // return int(x)
// // //
// // // Masking (& operations) can be left away when there's no
// // // danger that a field's sum will carry over into the next
// // // field: Since the result cannot be > 64, 8 bits is enough
// // // and we can ignore the masks for the shifts by 8 and up.
// // // Per "Hacker's Delight", the first line can be simplified
// // // more, but it saves at best one instruction, so we leave
// // // it alone for clarity.
// // const m = 1<<64 - 1
// // x = x>>1&(m0&m) + x&(m0&m)
// // x = x>>2&(m1&m) + x&(m1&m)
// // x = (x>>4 + x) & (m2 & m)
// // x += x >> 8
// // x += x >> 16
// // x += x >> 32
// // return int(x) & (1<<7 - 1)
// // }

// // // --- RotateLeft ---

// // // RotateLeft returns the value of x rotated left by (k mod [UintSize]) bits.
// // // To rotate x right by k bits, call RotateLeft(x, -k).
// // //
// // // This function's execution time does not depend on the inputs.
// // func RotateLeft(x uint, k int) uint {
// // if UintSize == 32 {
// // return uint(RotateLeft32(uint32(x), k))
// // }
// // return uint(RotateLeft64(uint64(x), k))
// // }

// // // RotateLeft8 returns the value of x rotated left by (k mod 8) bits.
// // // To rotate x right by k bits, call RotateLeft8(x, -k).
// // //
// // // This function's execution time does not depend on the inputs.
// // func RotateLeft8(x uint8, k int) uint8 {
// // const n = 8
// // s := uint(k) & (n - 1)
// // return x<<s | x>>(n-s)
// // }

// // // RotateLeft16 returns the value of x rotated left by (k mod 16) bits.
// // // To rotate x right by k bits, call RotateLeft16(x, -k).
// // //
// // // This function's execution time does not depend on the inputs.
// // func RotateLeft16(x uint16, k int) uint16 {
// // const n = 16
// // s := uint(k) & (n - 1)
// // return x<<s | x>>(n-s)
// // }

// // // RotateLeft32 returns the value of x rotated left by (k mod 32) bits.
// // // To rotate x right by k bits, call RotateLeft32(x, -k).
// // //
// // // This function's execution time does not depend on the inputs.
// // func RotateLeft32(x uint32, k int) uint32 {
// // const n = 32
// // s := uint(k) & (n - 1)
// // return x<<s | x>>(n-s)
// // }

// // // RotateLeft64 returns the value of x rotated left by (k mod 64) bits.
// // // To rotate x right by k bits, call RotateLeft64(x, -k).
// // //
// // // This function's execution time does not depend on the inputs.
// // func RotateLeft64(x uint64, k int) uint64 {
// // const n = 64
// // s := uint(k) & (n - 1)
// // return x<<s | x>>(n-s)
// // }

// // // --- Reverse ---

// // // Reverse returns the value of x with its bits in reversed order.
// // func Reverse(x uint) uint {
// // if UintSize == 32 {
// // return uint(Reverse32(uint32(x)))
// // }
// // return uint(Reverse64(uint64(x)))
// // }

// // // Reverse8 returns the value of x with its bits in reversed order.
// // func Reverse8(x uint8) uint8 {
// // return rev8tab[x]
// // }

// // // Reverse16 returns the value of x with its bits in reversed order.
// // func Reverse16(x uint16) uint16 {
// // return uint16(rev8tab[x>>8]) | uint16(rev8tab[x&255])<<8
// // }

// // // Reverse32 returns the value of x with its bits in reversed order.
// // func Reverse32(x uint32) uint32 {
// // const m = 1<<32 - 1
// // x = x>>1&(m0&m) | x&(m0&m)<<1
// // x = x>>2&(m1&m) | x&(m1&m)<<2
// // x = x>>4&(m2&m) | x&(m2&m)<<4
// // return ReverseBytes32(x)
// // }

// // // Reverse64 returns the value of x with its bits in reversed order.
// // func Reverse64(x uint64) uint64 {
// // const m = 1<<64 - 1
// // x = x>>1&(m0&m) | x&(m0&m)<<1
// // x = x>>2&(m1&m) | x&(m1&m)<<2
// // x = x>>4&(m2&m) | x&(m2&m)<<4
// // return ReverseBytes64(x)
// // }

// // // --- ReverseBytes ---

// // // ReverseBytes returns the value of x with its bytes in reversed order.
// // //
// // // This function's execution time does not depend on the inputs.
// // func ReverseBytes(x uint) uint {
// // if UintSize == 32 {
// // return uint(ReverseBytes32(uint32(x)))
// // }
// // return uint(ReverseBytes64(uint64(x)))
// // }

// // // ReverseBytes16 returns the value of x with its bytes in reversed order.
// // //
// // // This function's execution time does not depend on the inputs.
// // func ReverseBytes16(x uint16) uint16 {
// // return x>>8 | x<<8
// // }

// // // ReverseBytes32 returns the value of x with its bytes in reversed order.
// // //
// // // This function's execution time does not depend on the inputs.
// // func ReverseBytes32(x uint32) uint32 {
// // const m = 1<<32 - 1
// // x = x>>8&(m3&m) | x&(m3&m)<<8
// // return x>>16 | x<<16
// // }

// // // ReverseBytes64 returns the value of x with its bytes in reversed order.
// // //
// // // This function's execution time does not depend on the inputs.
// // func ReverseBytes64(x uint64) uint64 {
// // const m = 1<<64 - 1
// // x = x>>8&(m3&m) | x&(m3&m)<<8
// // x = x>>16&(m4&m) | x&(m4&m)<<16
// // return x>>32 | x<<32
// // }

0 comments on commit be82334

Please sign in to comment.