Go-- Programming Language

[Pages:283]Go-- Programming Language

Programming Languages and Translators Final Report Spring 2021

Chen Chen, Yuyan Ke, Yang Li, Arya Lingyu Zhao cc4351, yk2822, yl4111, lz2650

Contents

1 Introduction

4

1.1 Background

4

1.2 Goals

4

1.2.1 Familiarity

4

1.2.2 Lightweight Concurrency

4

1.2.3 Hide Pointers and Memory Management

4

2 Go-- Tutorial

5

2.1 Environment Setup

5

2.2 Compilation Guide

5

2.3 Language Tutorial

6

3 Language Reference Manual

7

3.1 Comments and Whitespace

7

3.1.1 Comments

7

3.1.2 Whitespace

8

3.2 Data Type

8

3.2.1 Primitive Data Types

8

3.2.1.1 Integer Types

8

3.2.1.2 Float Types

9

3.2.1.3 Boolean Types

9

3.2.1.4 String Types

9

3.2.2 The Void Keyword

10

3.3 Variables

10

3.3.1 Variable Naming and declarations

10

3.3.2 Scope of Variables

10

3.3.3 Variable Declaration and Assignment

11

3.4 Statement

11

3.4.1 Expression Statement

11

3.4.2 Compound Statement

13

3.4.3 Conditional Statement

13

3.4.4 While Statement

14

3.4.5 For Statement

14

3.4.6 Return Statement

15

3.5 Channel

15

3.5.1 Channel Data Structure

15

3.5.2 Channel Creation

15

1

3.5.3 Enqueue into channel (->)

16

3.5.4 Dequeue from channel (->)

17

3.6 Array

17

3.6.1 Declaring Array

17

3.6.2 Instantiating and Indexing Array

18

3.7 Structs

19

3.7.1 Struct Declaration

19

3.8 Arithmetic Operators

20

3.8.1 Order of Evaluation

20

3.8.2 Addition (+) and Subtraction (-) Operators

21

3.8.3 Multiplication (*) and Division (/) Operators

21

3.8.5 Boolean Operators (, =, !=, ==)

22

3.8.6 Logical Operators (!, &&, ||)

22

3.9 Functions

22

3.9.1 Function declaration

23

3.9.2 Concurrent Functions

24

3.10 Sample Program

25

4 Project Plan

29

4.1 Planning, Specification, Development, and Testing

29

4.2 Project Timeline

29

4.2.1 Planned Timeline

30

4.2.2 Actual Timeline

30

4.3 Team Roles and Responsibilities

31

5 Architecture Design

32

5.1 Scanner

33

5.2 Parser and AST

33

5.3 Semantic Checking

33

5.4 Code Generation

33

6 Design Choices and Language Revolution

34

6.1 Gofunction

34

6.2 channel

35

7 Testing

36

7.1 Pretty Print

36

7.2 Integration Tests

36

7.2.1 Test Automation and Scripts

37

7.2.2 Concurrency Test

37

2

8 Lessons Learned

39

8.1 Chen Chen

39

8.2 Yuyan Ke

39

8.3 Yang Li

40

8.4 Arya Lingyu Zhao

40

9 Appendix

41

9.1 Source Files

41

9.1.1 gmm.ml

41

9.1.2 scanner.mll

42

9.1.3 ast.ml

45

9.1.4 parser.mly

50

9.1.5 sast.ml

58

9.1.6 codegen.ml

62

9.1.7 builtin.c

84

9.1.8 Makefile

109

9.1.9 testall.sh

112

9.1.10 run.sh

119

9.2 Git Logs

123

9.3 Git Logs

215

3

1 Introduction

Go-- is an imperative, statically typed language with C-like syntax and support for concurrency. Inspired by Golang, Go-- also lightweight concurrency with gofunction, and uses channels for communications in between functions. Gofunction and channel enables the users to write concurrent programs with fewer lines of code, neater design, and provides better readability.

1.1 Background

C has long been the fundamental languages for lots of programming learners above the intro level. Yet, using the concurrency in C involves the understanding of pointers and memory management. Goroutine makes it much more user-friendly for writing concurrency in everyday programming. Goroutine is a lightweight thread, and the users don't need to understand pointers or manage runtime memory to use it. However, the overall syntax of Golang is quite different from C.

1.2 Goals

1.2.1 Familiarity

Go-- syntax is very similar to C language. C programmers can easily learn the syntax of Go-- and use it seamlessly. Go-- adds no additional difficulty to the learning curve. As for the concurrency features of Golang. The goroutine, channel and gofunction are syntactically similar to Golang.

1.2.2 Lightweight Concurrency

Go-- provides the lightweight concurrency, including features like channel, goroutine, and gofunction. Compared to C, the syntax of concurrency features are easier to understand, remember, and debug.

1.2.3 Hide Pointers and Memory Management

Go-- users don't need to understand pointers or memory management to use the concurrency. It's a deep relief for lots of programming learners. Using Go--, users can write less and more succinctly.

4

2 Go-- Tutorial

2.1 Environment Setup

Clone from Go-- git repo and go to the corresponding folder > git clone > cd Go-Run docker image > docker run --rm -it -v `pwd`:/home/gmm -w=/home/gmm columbiasedwards/plt

2.2 Compilation Guide

Next, compile the Go-- compiler using the following commands. This will automatically run Go--'s test suite as well Compile the Go-- compiler > make all Run all test cases > ./testall.sh Or you can compile the compiler, and then run the test suite in separate steps using the following command in Go-- folder: > make When you run either of the two sets of commands above, all the tests that came with the compiler will be run and should pass. If they do not all pass, go back to section 2.1 Environment Setup and ensure that your environment is set up correctly.

5

If you want to run a single test case in the tests folder: > ./misc/run.sh ./tests/.gmm And to remove the generated files from compiling and running tests > make clean To compile and run a simple program > ./misc/run.sh ./path/to/.gmm And to remove the generated executable and intermediate files > make clean

2.3 Language Tutorial

We go through the steps here to write, compile, and run a simple program. sample.gmm function int add(int x, int y) { return x + y; } function int main() { print( add(17, 25) ); return 0; }

6

> ./misc/run.sh ./sample.gmm

The program above will output sample... ###### Testing sample ./gmm.native sample.gmm > sample.ll llc -relocation-model=pic sample.ll > sample.s cc -o sample.exe sample.s -pthread ./src/builtin.o ./sample.exe 42

3 Language Reference Manual

3.1 Comments and Whitespace

3.1.1 Comments The characters /* introduce a comment, which terminates with the characters */. Comments can be nested as long as the opening / and closing / are all matched. /* single-line comment */

/* multi-line comment 7

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download