提交 708fa769 编写于 作者: chai2010's avatar chai2010

全局用 global 关键字

上级 5024b8a3
......@@ -2,7 +2,7 @@
import "syscall/arduino"
var LED = arduino.GetPinLED()
global LED = arduino.GetPinLED()
func init {
arduino.PinMode(LED, arduino.OUTPUT)
......
......@@ -2,7 +2,7 @@
import "syscall/arduino"
var LED = arduino.GetPinLED()
global LED = arduino.GetPinLED()
func init {
arduino.PinMode(LED, 1)
......
// 版权 @2021 凹语言 作者。保留所有权利。
var s = []int{1, 2, 3, 4, 5, 6}
global s = []int{1, 2, 3, 4, 5, 6}
func main() {
d := make([]int, 3)
......
......@@ -6,7 +6,7 @@
func write_port(p: i16, v: i8)
// Delay a period of time
var cnt: i8
global cnt: i8
func delay(n: i32) {
for i := i32(0); i < n; i++ {
......
......@@ -5,8 +5,8 @@ const size = 32
type fff32 f32
var arr0: [size]fff32
var arr1: [size]fff32
global arr0: [size]fff32
global arr1: [size]fff32
func test_init() {
for i := 0; i < size; i++ {
......
......@@ -5,7 +5,7 @@ const size = 32
type fff32 f32
var arr1: [size]fff32
global arr1: [size]fff32
func makearr0a(i: int) => [size]fff32 {
r: [size]fff32
......
// Test the llvm backend.
// Test bitwise logic.
var a: i64 = 0x55555555
var b: i64 = 0x33333333
var c: i16 = 0x5555
var d: i16 = 0x3333
var e: u8 = 0x55
var f: u8 = 0x33
global a: i64 = 0x55555555
global b: i64 = 0x33333333
global c: i16 = 0x5555
global d: i16 = 0x3333
global e: u8 = 0x55
global f: u8 = 0x33
func main() {
println("not ", 0x55555555, " = ", bw_not(0x55555555))
......
......@@ -6,8 +6,8 @@ type ty0 struct {
v1: f64
}
var gv0: ty0 = ty0{5555, 3.1415926}
var gv1: [4]int = [4]int{81, 82, 17, 76}
global gv0: ty0 = ty0{5555, 3.1415926}
global gv1: [4]int = [4]int{81, 82, 17, 76}
func test_global_variable() {
println("gv0: {", gv0.v0, ", ", gv0.v1, "}")
......
......@@ -6,8 +6,8 @@ type ty0 struct {
v1: f64
}
var gv0: ty0 = ty0{5555, 3.1415926}
var gv1: [4]int = [4]int{81, 82, 17, 76}
global gv0: ty0 = ty0{5555, 3.1415926}
global gv1: [4]int = [4]int{81, 82, 17, 76}
func test_global_variable() {
gv0.v0 = 8888
......
// Test the llvm backend.
// Test read/write via a pointer.
var gint0: int = 100
var gint1: int = 200
global gint0: int = 100
global gint1: int = 200
func get_int_addr(a: int) => *int {
if a > 0 {
......
// Test the llvm backend.
// Test logical/arithmetic shift operations.
var ga: i32 = -15
var gb: i32 = 15
var gc: i64 = -15
var gd: i64 = 15
var ge: u16 = 0xffcc
global ga: i32 = -15
global gb: i32 = 15
global gc: i64 = -15
global gd: i64 = 15
global ge: u16 = 0xffcc
func main() {
func main {
println(ga, " << ", 2, " = ", test_shl_0(ga, 2))
println(gb, " << ", 2, " = ", test_shl_1(gb, 2))
println(ga, " << ", 2, " = ", test_shl_2(ga, 2))
......
......@@ -10,7 +10,7 @@ type struct_t0 struct {
arr1: [size]fff32
}
var st0: struct_t0
global st0: struct_t0
func test_init() {
for i := 0; i < size; i++ {
......
......@@ -8,7 +8,7 @@ type struct_t0 struct {
arr1: [size]f32
}
var st1: struct_t0
global st1: struct_t0
func maket0a(i: int) => struct_t0 {
r: struct_t0
......
// 版权 @2021 凹语言 作者。保留所有权利。
var I: [8]i32
global I: [8]i32
func Gen_arr(param: [8]i32) => [8]i32 {
param[3] += 2
......
// 版权 @2021 凹语言 作者。保留所有权利。
var counter: i32 = 13
var G: *i32
global counter: i32 = 13
global G: *i32
func gen_i() => *i32 {
i: i32
......
......@@ -9,7 +9,7 @@ type ST struct {
B: i32
}
var G: ST
global G: ST
func Do {
G.A = "13"
......
......@@ -5,7 +5,7 @@ type ST struct {
B: string
}
var G: ST
global G: ST
func Do(x: i32) {
G.A = x
......
......@@ -37,7 +37,7 @@ const (
)
func main {
runtime_printBytes([]byte{ 'a', 'b', '\n'})
runtime_printBytes([]byte{'a', 'b', '\n'})
runtime_printString("hello\n")
runtime_printString("== printBool ==\n")
......@@ -65,7 +65,7 @@ func main {
runtime_printNewline()
runtime_printU64(MaxUint64)
runtime_printNewline()
runtime_printU64(MaxUint64/2)
runtime_printU64(MaxUint64 / 2)
runtime_printNewline()
runtime_printString("== printHex ==\n")
......
......@@ -85,7 +85,7 @@ func Canvas.Flush() {
updateCanvas_JS(this.device_id, &this.frame_buf[0])
}
var canvas_events: []CanvasEvents
global canvas_events: []CanvasEvents
func AttachCanvasEvents(e: CanvasEvents) {
for i := range canvas_events {
......
......@@ -2,7 +2,7 @@
import "snake/canvas"
var ca: *canvas.Canvas
global ca: *canvas.Canvas
type Position struct {
x, y: i32
......@@ -26,7 +26,7 @@ const (
DirDown
)
var Dirs: [5]Position
global Dirs: [5]Position
type GameState struct {
w, h: i32
......@@ -38,7 +38,7 @@ type GameState struct {
ca: *canvas.Canvas
}
var gameState: GameState
global gameState: GameState
func GameState.Init(w, h: i32, scale: i32) {
this.w = w
......
// 版权 @2023 凹语言 作者。保留所有权利。
var Info: struct {
global Info: struct {
name: string
age: i32
}
......
......@@ -11,7 +11,7 @@ const (
angle = math.Pi / 6 // angle of x, y axes (=30°)
)
var sin30, cos30 = math.Sin(angle), math.Cos(angle) // sin(30°), cos(30°)
global sin30, cos30 = math.Sin(angle), math.Cos(angle) // sin(30°), cos(30°)
func main {
print("<svg xmlns='http://www.w3.org/2000/svg' style='stroke: grey; fill: white; stroke-width: 0.7' ")
......
......@@ -7,7 +7,7 @@ func main {
}
func sum(n: int) => int {
var v: int
v: int
for i := 1; i <= n; i++ {
v += i
}
......
......@@ -37,10 +37,10 @@ type ByteOrder interface {
}
// LittleEndian is the little-endian implementation of ByteOrder.
var LittleEndian = &littleEndian{}
global LittleEndian = &littleEndian{}
// BigEndian is the big-endian implementation of ByteOrder.
var BigEndian = &bigEndian{}
global BigEndian = &bigEndian{}
type littleEndian struct{}
......@@ -392,7 +392,7 @@ func Size(v interface{}) int {
return dataSize(reflect.Indirect(reflect.ValueOf(v)))
}
var structSize sync.Map // map[reflect.Type]int
global structSize sync.Map // map[reflect.Type]int
// dataSize returns the number of bytes the actual data represented by v occupies in memory.
// For compound structures, it sums the sizes of the elements. Thus, for instance, for a slice
......
......@@ -23,7 +23,7 @@ func TestEqualFold {
}
}
var EqualFoldTests = []struct {
global EqualFoldTests = []struct {
s, t string
out bool
}{
......@@ -35,7 +35,7 @@ var EqualFoldTests = []struct {
{"abcdefghijk", "abcdefghijX", false},
}
var compareTests = []struct {
global compareTests = []struct {
a, b []byte
i int
}{
......
......@@ -9,7 +9,7 @@ import (
// ErrUnsupported means that the input BMP image uses a valid but unsupported
// feature.
var ErrUnsupported = errors.New("bmp: unsupported BMP image")
global ErrUnsupported = errors.New("bmp: unsupported BMP image")
func readUint16(b: []byte) => uint16 {
return uint16(b[0]) | uint16(b[1])<<8
......
......@@ -16,7 +16,7 @@ import "image/color"
//
// This palette was used in the Plan 9 Operating System, described at
// https://9p.io/magic/man2html/6/color
var Plan9 = []color.Color{
global Plan9 = []color.Color{
&color.RGBA{0x00, 0x00, 0x00, 0xff},
&color.RGBA{0x00, 0x00, 0x44, 0xff},
&color.RGBA{0x00, 0x00, 0x88, 0xff},
......@@ -279,7 +279,7 @@ var Plan9 = []color.Color{
// of Netscape Navigator. It is also known as the Netscape Color Cube.
//
// See https://en.wikipedia.org/wiki/Web_colors#Web-safe_colors for details.
var WebSafe = []color.Color{
global WebSafe = []color.Color{
&color.RGBA{0x00, 0x00, 0x00, 0xff},
&color.RGBA{0x00, 0x00, 0x33, 0xff},
&color.RGBA{0x00, 0x00, 0x66, 0xff},
......
......@@ -65,7 +65,7 @@ func (p: Point) Eq(q: Point) => bool {
// ZP is the zero Point.
//
// Deprecated: Use a literal image.Point{} instead.
var ZP: Point
global ZP: Point
// Pt is shorthand for Point{X, Y}.
func Pt(X, Y: int) => Point {
......@@ -264,7 +264,7 @@ func (r: Rectangle) ColorModel => color.Model {
// ZR is the zero Rectangle.
//
// Deprecated: Use a literal image.Rectangle{} instead.
var ZR: Rectangle
global ZR: Rectangle
// Rect is shorthand for Rectangle{Pt(x0, y0), Pt(x1, y1)}. The returned
// rectangle has minimum and maximum coordinates swapped if necessary so that
......
......@@ -1230,7 +1230,7 @@ func (p *Paletted) SubImage(r Rectangle) Image {
// Opaque scans the entire image and reports whether it is fully opaque.
func (p *Paletted) Opaque() bool {
var present [256]bool
present: [256]bool
i0, i1 := 0, p.Rect.Dx()
for y := p.Rect.Min.Y; y < p.Rect.Max.Y; y++ {
for _, c := range p.Pix[i0:i1] {
......
......@@ -11,13 +11,13 @@ const (
// ErrShortWrite means that a write accepted fewer bytes than requested
// but failed to return an explicit error.
var ErrShortWrite = errors.New("short write")
global ErrShortWrite = errors.New("short write")
// errInvalidWrite means that a write returned an impossible count.
var errInvalidWrite = errors.New("invalid write result")
global errInvalidWrite = errors.New("invalid write result")
// ErrShortBuffer means that a read required a longer buffer than was provided.
var ErrShortBuffer = errors.New("short buffer")
global ErrShortBuffer = errors.New("short buffer")
// EOF is the error returned by Read when no more input is available.
// (Read must return EOF itself, not an error wrapping EOF,
......@@ -26,16 +26,16 @@ var ErrShortBuffer = errors.New("short buffer")
// If the EOF occurs unexpectedly in a structured data stream,
// the appropriate error is either ErrUnexpectedEOF or some other error
// giving more detail.
var EOF = errors.New("EOF")
global EOF = errors.New("EOF")
// ErrUnexpectedEOF means that EOF was encountered in the
// middle of reading a fixed-size block or data structure.
var ErrUnexpectedEOF = errors.New("unexpected EOF")
global ErrUnexpectedEOF = errors.New("unexpected EOF")
// ErrNoProgress is returned by some clients of an Reader when
// many calls to Read have failed to return any data or error,
// usually the sign of a broken Reader implementation.
var ErrNoProgress = errors.New("multiple Read calls return no data or error")
global ErrNoProgress = errors.New("multiple Read calls return no data or error")
// Reader is the interface that wraps the basic Read method.
//
......@@ -309,7 +309,7 @@ func ReadAtLeast(r: Reader, buf: []byte, min: int) => (n: int, err: error) {
return 0, ErrShortBuffer
}
for n < min && err == nil {
var nn :int
global nn :int
nn, err = r.Read(buf[n:])
n += nn
}
......@@ -487,8 +487,8 @@ func SectionReader.Read(p: []byte) => (n: int, err: error) {
return
}
var errWhence = errors.New("Seek: invalid whence")
var errOffset = errors.New("Seek: invalid offset")
global errWhence = errors.New("Seek: invalid whence")
global errOffset = errors.New("Seek: invalid offset")
func SectionReader.Seek(offset: i64, whence: int) => (i64, error) {
switch whence {
......@@ -553,13 +553,13 @@ func teeReader.Read(p: []byte) => (n: int, err: error) {
// Discard is a Writer on which all Write calls succeed
// without doing anything.
var Discard :Writer = &discard{}
global Discard :Writer = &discard{}
type discard struct{}
// discard implements ReaderFrom as an optimization so Copy to
// io.Discard can avoid doing unnecessary work.
var _ :ReaderFrom = &discard{}
global _ :ReaderFrom = &discard{}
func discard.Write(p: []byte) => (int, error) {
return len(p), nil
......
// 版权 @2023 凹语言 作者。保留所有权利。
#wa:linkname $waFloat64bits
#wa:linkname $math.waFloat64bits
func waFloat64bits(x: f64) => u64
#wa:linkname $waFloat64frombits
#wa:linkname $math.waFloat64frombits
func waFloat64frombits(x: u64) => f64
#wa:linkname $waFloat32bits
#wa:linkname $math.waFloat32bits
func waFloat32bits(x: f32) => u32
#wa:linkname $waFloat32frombits
#wa:linkname $math.waFloat32frombits
func waFloat32frombits(x: u32) => f32
// Float64bits returns the IEEE 754 binary representation of f,
......
;; Copyright 2023 The Wa Authors. All rights reserved.
(func $$waFloat64bits (param $x f64) (result i64)
(func $$math.waFloat64bits (param $x f64) (result i64)
local.get $x
i64.reinterpret_f64
)
(func $$waFloat64frombits (param $x i64) (result f64)
(func $$math.waFloat64frombits (param $x i64) (result f64)
local.get $x
f64.reinterpret_i64
)
(func $$waFloat32bits (param $x f32) (result i32)
(func $$math.waFloat32bits (param $x f32) (result i32)
local.get $x
i32.reinterpret_f32
)
(func $$waFloat32frombits (param $x i32) (result f32)
(func $$math.waFloat32frombits (param $x i32) (result f32)
local.get $x
f32.reinterpret_i32
)
\ No newline at end of file
)
// 版权 @2023 凹语言 作者。保留所有权利。
var Args: []string
global Args: []string
#wa:linkname runtime.os_get_args
func os_get_args() => []string
......
......@@ -6,7 +6,7 @@ func TestMatch {
}
}
var tests = []struct {
global tests = []struct {
name :string
regexp :string
text :string
......
// 版权 @2023 凹语言 作者。保留所有权利。
// 十进制整数 x 转为字符串
func __Itoa(x: int) => string {
return __itoa(x)
}
func __itoa(num: int) => string {
if num == 0 {
return "0"
}
var isNegative = false
var str: string
// 用于 byte 到 string 转型
var digit2StrTable = []string{
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
}
if num < 0 {
isNegative = true
num = -num
}
for ; num != 0; num = num / 10 {
str = digit2StrTable[num%10] + str
}
if isNegative {
return "-" + str
}
return str
}
// 版权 @2022 凹语言 作者。保留所有权利。
var (
const (
LOW :i32 = 0
HIGH :i32 = 1
......
......@@ -16,13 +16,13 @@ const (
)
// GraphicRanges defines the set of graphic characters according to Unicode.
var GraphicRanges = []*RangeTable{
global GraphicRanges = []*RangeTable{
L, M, N, P, S, Zs,
}
// PrintRanges defines the set of printable characters according to Go.
// ASCII space, U+0020, is handled separately.
var PrintRanges = []*RangeTable{
global PrintRanges = []*RangeTable{
L, M, N, P, S,
}
......
......@@ -59,7 +59,7 @@ const (
)
// first is information about the first byte in a UTF-8 sequence.
var first = [256]u8{
global first = [256]u8{
// 1 2 3 4 5 6 7 8 9 A B C D E F
as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x00-0x0F
as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x10-0x1F
......@@ -90,7 +90,7 @@ type acceptRange struct {
}
// acceptRanges has size 16 to avoid bounds checks in the code that uses it.
var acceptRanges = [16]acceptRange{
global acceptRanges = [16]acceptRange{
0: {locb, hicb},
1: {0xA0, hicb},
2: {locb, 0x9F},
......@@ -385,7 +385,7 @@ func EncodeRune(p: []byte, r: rune) => int {
// encodings are treated as single runes of width 1 byte.
func RuneCount(p: []byte) => int {
np := len(p)
var n :int
n :int
for i := 0; i < np; {
n++
c := p[i]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册