Skip to content

Commit f77ae14

Browse files
authored
Structs (#42)
* Implement field assignment for anomyous structs * Implement struct initialisation and fix concurrency * implement reassignment of struct field values * Deepcopy for arrays when passed as arguments for functions * Implement nested struct for anonymous structs * Implement some support for nested structs * Implement arrays of structs * Implement array and struct interactions * Implement array and struct interactions * Implement pointers * Implement support of pointers for structs * Implement alignof, offsetof, sizeof * Implement Add, StringData, String * Add test coverage utility * Cleanup codebase * Update readme.md and fix deepcopy not being applied on goroutines * Change yarn run local to yarn local * Attempt to fix compilation/installation errors * Delete eslint-import related dependencies * Refactoring, incomplete * Refactoring * Fix logic for multi-dimensional arrays containing structs * Minor cleanup * Fix literal values not being allowed for struct fields with declared type * Fix cases where arrays/structs being instantiated and immediately used as arguments do not work * Further fix to recognise different structs when multiple directly created structs are passed as arguments * Fix test case * Fix nested struct not using key index * Fix test file * Code cleanup * Change struct to array, it should be array * Improve automatic semicolon insertion
1 parent a779f41 commit f77ae14

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+10953
-3854
lines changed

.eslintignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ build
44
node_modules
55

66
# Ignore the parser generated by PeggyJS.
7-
src/virtual-machine/parser/*.js
7+
./src/go-virtual-machine-main/virtual-machine/compiler/golang_parser.js
8+
9+
# Ignore test files
10+
./src/go-virtual-machine-main/tests/*.test.ts

.eslintrc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@
2525
}
2626
}
2727
],
28-
"ignorePatterns": ["build/**/*", "!.storybook", "tsconfig.json"],
28+
"ignorePatterns": [
29+
"build/**/*",
30+
"!.storybook",
31+
"tsconfig.json",
32+
"**/golang_parser.js"
33+
],
2934
"env": { "es6": true },
3035
"parserOptions": {
3136
"ecmaVersion": 2018,

.vscode/launch.json

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
{
2-
// Use IntelliSense to learn about possible attributes.
3-
// Hover to view descriptions of existing attributes.
4-
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5-
"version": "0.2.0",
6-
"configurations": [
7-
{
8-
"type": "node",
9-
"request": "launch",
10-
"name": "Launch Program",
11-
"skipFiles": [
12-
"<node_internals>/**"
13-
],
14-
"program": "${workspaceFolder}\\src\\index.tsx",
15-
"outFiles": [
16-
"${workspaceFolder}/**/*.js"
17-
]
18-
}
19-
]
20-
}
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "node",
9+
"request": "launch",
10+
"name": "Launch Program",
11+
"skipFiles": ["<node_internals>/**"],
12+
"program": "${workspaceFolder}\\src\\index.tsx",
13+
"outFiles": ["${workspaceFolder}/**/*.js"]
14+
}
15+
]
16+
}

README.md

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,77 @@
11
# go-slang
2-
Implementation of Go
32

4-
# Virtual Machine
5-
The virtual machine is used to run programs written in Go. It is written by Teow Hua Jun and Lim An Jun and the source code can be found [here](https://github.com/huajun07/go-virtual-machine).
3+
This is an implementation of the language Go, which uses a Virtual Machine architecture and is written in TypeScript.
4+
5+
The virtual machine is used to run programs written in Go. The basic implementation was written by Teow Hua Jun and Lim An Jun and the source code can be found [here](https://github.com/huajun07/go-virtual-machine).
6+
7+
After which, this project is further enhanced by bringing in more features.
8+
9+
## Usage
10+
11+
To begin, we need to install the package manager `yarn`, which can be installed using another package manager `npm`.
12+
13+
```sh
14+
npm install yarn
15+
```
16+
17+
After which, we use `yarn install` to install the dependencies used by this repository.
18+
19+
```sh
20+
yarn install
21+
```
22+
23+
For testing, we can use `yarn test` to run all the test cases.
24+
25+
```sh
26+
yarn test
27+
```
28+
29+
To run a specific test file, we use `yarn test <fileName>`, where the test file must be named as: `<fileName>.test.ts`. For example, to run `unsafe.test.ts`, we use `yarn test unsafe`. This will also work for new test files, so long as the test file is named correctly.
30+
31+
```sh
32+
yarn test unsafe
33+
```
34+
35+
To run tests with coverage, we use `yarn coverage`. This will display the test coverage (statement coverage, line coverage, branch coverage and function coverage) after running all tests.
36+
37+
For visualisation, we can use `yarn local` to run the program locally. This lets us visualise the program that is to be written in Go to let us see how the program is executed. However, this only works when the program does not run into any errors.
38+
39+
```sh
40+
yarn local
41+
```
42+
43+
Lastly, to build the project, we use `yarn build`.
44+
45+
```sh
46+
yarn build
47+
```
48+
49+
## Major architectures
50+
51+
This virtual machine is based on the [Go Language Specification](https://go.dev/ref/spec). It ensures that the implementation can support the notations stated in the specification, which is used by the actual language.
52+
53+
To parse the user program written in Go into the virtual machine, [PeggyJS](https://peggyjs.org/) is used. The user program is first parsed into an Abstract Syntax Tree (AST) using PeggyJS, then it is compiled into virtual machine instructions, which are then executed by the runtime system of the virtual machine.
54+
55+
The runtime system primarily operates on Nodes (not to be confused with NodeJS), which are the objects recognised by the runtime system to load data stored on the heap. The base class is called BaseNode instead of Node as a result (Node class actually refers to NodeJS, which is not what it is supposed to be). These objects are basically pointers by themselves.
56+
57+
## Newly added features
58+
59+
Compared to the previous version, new features of the language are added. This includes:
60+
61+
- Functions being able to return multiple values at once without needing to wrap them into a data structure
62+
- Structs
63+
- Pointers (including the `unsafe` package)
64+
65+
There are also fundamental changes implemented, such as:
66+
67+
- Ensuring contiguous memory for arrays and structs
68+
- Deepcopy for arrays and structs being passed by value into other functions
69+
- Deepcopy for variables being passed into goroutines
70+
71+
More features will be implemented as time passes.
72+
73+
## Additional Resources on Go
74+
75+
We recommend the [Tour of Go](https://go.dev/tour/welcome/1), which is written by the developers of the language themselves. It introduces the various elements of the programming language as well as their syntaxes, as the syntaxes may differ quite greatly from other programming languages.
76+
77+
For further exploration, we can download the language Go itself [here](https://go.dev/doc/install).

package.json

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"dev": "NODE_ENV=dev && concurrently \"npm run generate-parser-watch\" \"react-scripts start\"",
99
"build": "npm run generate-parser && react-scripts build",
1010
"test": "npm run generate-parser && vitest",
11+
"coverage": "npm run generate-parser && vitest --ui --config ./vitest.config.js",
1112
"eject": "react-scripts eject",
1213
"lint-no-fix": "eslint ./ --ignore-path .gitignore && prettier . -c",
1314
"lint": "eslint ./ --ignore-path .gitignore --fix && prettier . -c --write",
@@ -17,18 +18,36 @@
1718
"author": "",
1819
"license": "ISC",
1920
"dependencies": {
21+
"@babel/core": "^7.0.0",
22+
"@babel/plugin-syntax-flow": "^7.14.5",
23+
"@babel/plugin-transform-react-jsx": "^7.14.5",
24+
"@babel/runtime": "^7.11.0",
2025
"@chakra-ui/icons": "^2.0.19",
2126
"@chakra-ui/react": "^2.6.1",
27+
"@chakra-ui/react-utils": "^2.0.5",
28+
"@chakra-ui/styled-system": "^2.8.0",
29+
"@chakra-ui/system": "^2.0.0",
2230
"@chakra-ui/theme": "^3.1.1",
2331
"@chakra-ui/theme-tools": "^2.0.17",
32+
"@chakra-ui/utils": "^2.0.8",
2433
"@choc-ui/chakra-autocomplete": "^5.1.7",
34+
"@codemirror/autocomplete": "^6.0.0",
2535
"@codemirror/lang-go": "^6.0.0",
36+
"@codemirror/language": "^6.0.0",
37+
"@codemirror/lint": "^6.0.0",
38+
"@codemirror/search": "^6.0.0",
39+
"@codemirror/state": "^6.0.0",
40+
"@codemirror/theme-one-dark": "^6.0.0",
41+
"@codemirror/view": "^6.0.0",
2642
"@emotion/react": "^11.11.0",
2743
"@emotion/styled": "^11.11.0",
44+
"@lezer/common": "^1.0.0",
2845
"@uiw/codemirror-extensions-zebra-stripes": "^4.20.2",
2946
"@uiw/codemirror-theme-github": "^4.20.2",
3047
"@uiw/react-codemirror": "^4.21.25",
3148
"axios": "^1.7.4",
49+
"codemirror": "^6.0.0",
50+
"cytoscape": "^3.2.19",
3251
"cytoscape-cola": "^2.5.1",
3352
"d3-selection": "^3.0.0",
3453
"d3-zoom": "^3.0.0",
@@ -58,12 +77,12 @@
5877
"@types/react-dom": "^18.2.4",
5978
"@types/seedrandom": "^3.0.8",
6079
"@types/uuid": "^9.0.2",
80+
"@vitest/coverage-v8": "^3.1.1",
6181
"babel-plugin-named-exports-order": "^0.0.2",
6282
"chromatic": "^6.19.9",
6383
"concurrently": "^8.2.2",
6484
"eslint": "^8.41.0",
6585
"eslint-config-prettier": "^8.8.0",
66-
"eslint-import-resolver-alias": "^1.1.2",
6786
"eslint-plugin-prettier": "^4.2.1",
6887
"eslint-plugin-simple-import-sort": "^10.0.0",
6988
"eslint-plugin-storybook": "^0.6.12",
@@ -73,7 +92,7 @@
7392
"start-server-and-test": "^2.0.0",
7493
"tsconfig-paths-webpack-plugin": "^4.1.0",
7594
"typescript": "^5.0.4",
76-
"vitest": "^1.6.1",
95+
"vitest": "^3.1.1",
7796
"webpack": "^5.94.0"
7897
},
7998
"overrides": {

src/frontend/pages/Main.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export const Main = () => {
4444
const [speed, setSpeed] = useState<number>(1)
4545
const [loading, setLoading] = useState(false)
4646
const [code, setCode] = useState('')
47-
const [heapsize, setHeapsize] = useState(2048)
47+
const [heapsize, setHeapsize] = useState(4096)
4848
const [visualMode, setVisualMode] = useState(false)
4949

5050
useEffect(() => {

src/frontend/stores/executionStore.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import { shallow } from 'zustand/shallow'
22
import { createWithEqualityFn } from 'zustand/traditional'
33

4-
import { ContextInfo, StateInfo } from '../../go-virtual-machine-main/virtual-machine/runtime/debugger'
54
import { TokenLocation } from '../../go-virtual-machine-main/virtual-machine/compiler/tokens'
5+
import {
6+
ContextInfo,
7+
StateInfo,
8+
} from '../../go-virtual-machine-main/virtual-machine/runtime/debugger'
69

710
export interface ExecutionState {
811
currentStep: number

0 commit comments

Comments
 (0)