This repository has been archived by the owner on Jun 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
semantic.go
139 lines (122 loc) · 2.73 KB
/
semantic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"fmt"
"strconv"
)
//This file contains code for semantic checking
//(ensure correct data types, syntax etc)
// The data type validator functions return empty
// errors for now since the error is dependent
// on the respective node in the AST (ast.go)
func AssertString(x *node, item string) (string, error) {
valInf, err := (*x).execute()
if err != nil {
return "", err
}
val, ok := valInf.(string)
if !ok {
return "", fmt.Errorf(item + " should be a string value")
}
return val, nil
}
func AssertInt(x *node, item string) (int, error) {
valInf, err := (*x).execute()
if err != nil {
return -1, err
}
val, ok := valInf.(int)
if !ok {
return -1, fmt.Errorf(item + " should be an int value")
}
return val, nil
}
func AssertFloat64(x *node, item string) (float64, error) {
valInf, err := (*x).execute()
if err != nil {
return -1, err
}
val, ok := valInf.(float64)
if !ok {
return -1, fmt.Errorf(item + " should be a float value")
}
return val, nil
}
func AssertFloat64Arr(x *node, item string) ([]float64, error) {
valInf, err := (*x).execute()
if err != nil {
return nil, err
}
val, ok := valInf.([]float64)
if !ok {
return nil, fmt.Errorf(item + " should be a float array")
}
return val, nil
}
func AssertBool(x *node, item string) (bool, error) {
valInf, err := (*x).execute()
if err != nil {
return false, err
}
val, ok := valInf.(bool)
if !ok {
return false, fmt.Errorf(item + " should be a boolean value")
}
return val, nil
}
// Check if x is a certain value
func AssertInStringValues(x interface{}, potential []string) bool {
for i := range potential {
if x == potential[i] {
return true
}
}
return false
}
func AssertColor(color interface{}) (string, bool) {
var colorStr string
if IsString(color) || IsInt(color) || IsFloat(color) {
if IsString(color) {
colorStr = color.(string)
}
if IsInt(color) {
colorStr = strconv.Itoa(color.(int))
}
if IsFloat(color) {
colorStr = strconv.FormatFloat(color.(float64), 'f', -1, 64)
}
for len(colorStr) < 6 {
colorStr = "0" + colorStr
}
if len(colorStr) != 6 {
return "", false
}
if !IsHexString(colorStr) {
return "", false
}
} else {
return "", false
}
return colorStr, true
}
func AssertInfArr(x *node, item string) ([]interface{}, error) {
valInf, err := (*x).execute()
if err != nil {
return nil, err
}
val, ok := valInf.([]interface{})
if !ok {
return nil, fmt.Errorf(item + " should be an array value")
}
return val, nil
}
func AssertStringArr(x *node, item string) ([]string, error) {
valInf, err := (*x).execute()
if err != nil {
return nil, err
}
val, ok := valInf.([]string)
if !ok {
return nil, fmt.Errorf(item + " should be an array value")
}
return val, nil
}