-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsemantics.go
More file actions
90 lines (82 loc) · 2.82 KB
/
semantics.go
File metadata and controls
90 lines (82 loc) · 2.82 KB
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
package main
import (
"fmt"
"github.com/timtadh/lexmachine"
"log"
)
type AbstractGlobalVariable struct {
Value string
Type string
}
type AbstractLocalVariable struct {
Value string
Type string
Name string
State string // Has 2 states, either pristine (untouched, will throw an error if it reahes end of program without being used) or dirty(used in the code afterwards, this is the normal accepted behavior)
}
type FunctionSignature struct {
Name string
ReturnType string
ParamList []AbstractLocalVariable
}
func NewFunctionSignature(name string, returnType string, paramList []AbstractLocalVariable) *FunctionSignature {
return &FunctionSignature{Name: name, ReturnType: returnType, ParamList: paramList}
}
var CheckVariableHierarchyQueue []*Node
var GlobalVars = make(map[string]*AbstractGlobalVariable)
var LocalVars = make(map[string]*AbstractLocalVariable)
var FunctionDeclarations = make(map[string]*FunctionSignature)
func NewAbstractGlobalVariable(node *Node) *AbstractGlobalVariable {
name, value, varType := destructureElementsFromVarDeclaration(node)
constructedNode := &AbstractGlobalVariable{
Value: value,
Type: varType,
}
GlobalVars[name] = constructedNode
return constructedNode
}
func NewAbstractLocalVariable(val string, typ string, name string) *AbstractLocalVariable {
return &AbstractLocalVariable{
Value: val,
Type: typ,
Name: name,
State: "Pristine",
}
}
func destructureElementsFromVarDeclaration(node *Node) (name string, value string, varType string) {
childrenCount := len(node.Children)
tokenList := make([]*lexmachine.Token, childrenCount)
for i, tok := range node.Children {
tokenList[i] = tok.Token // [int x = 5 ; ]
}
if childrenCount > 3 { // uninitialized var
return tokenList[1].Value.(string), tokenList[3].Value.(string), tokenList[0].Value.(string)
} else {
return tokenList[1].Value.(string), "", tokenList[0].Value.(string)
}
}
func (n *Node) AddContext(node *Node) {
name, value, varType := destructureElementsFromVarDeclaration(node)
constructedNode := &AbstractLocalVariable{
Value: value,
Type: varType,
}
n.Context[name] = constructedNode
}
// TODO: Try to find a way to backtrack to the nearest parent context.
func QueueElementForChecking(node *Node) {
CheckVariableHierarchyQueue = append(CheckVariableHierarchyQueue, node)
}
// FindHealthyOccurenceInParentContexts Function that recursively checks for the variable existance within the parent scope until it reaches the parent = nil
func FindHealthyOccurenceInParentContexts(node *Node, name string) {
if node.Parent == nil {
if _, ok := node.Context[name]; !ok {
log.Fatal(fmt.Errorf("variable %s is not defined previously", name))
return
} else {
return
}
} else { // Incase parent exists, recurse and keep looking for occurence.
FindHealthyOccurenceInParentContexts(node.Parent, name)
}
}