# sprite
**Repository Path**: greenflute/sprite
## Basic Information
- **Project Name**: sprite
- **Description**: The sprite repo
- **Primary Language**: Unknown
- **License**: GPL-3.0
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 3
- **Created**: 2024-02-07
- **Last Updated**: 2024-02-07
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# Sprite Lang
> Note the language syntax is subject to change quickly as the language is still in development.
# TODO
- [ ] 短期目标 能用sprite写一些简单算法,能画桃形图案,暂定代码(语法后期可能会改变)是这样:
```
func gcd(a int, b int) int {
if b == 0 {
return a
}
return gcd(b, a%b)
}
func binarySearch(arr []int, left int, right int, x int) int {
if right >= left {
var mid = left + (right - left) / 2
if arr[mid] == x {
return mid
}
if arr[mid] > x {
return binarySearch(arr, left, mid - 1, x)
}
return binarySearch(arr, mid + 1, right, x)
}
return -1
}
func ceil(a int, b int) int {
return (a + b - 1) / b
}
func floor(a int, b int) int {
return a / b
}
func fastSqrt(x int) int {
if x == 0 || x == 1 {
return x
}
var start = 1
var end = x
var ans = 0
for start <= end {
var mid = (start + end) / 2
if mid * mid == x {
return mid
}
if mid * mid < x {
start = mid + 1
ans = mid
} else {
end = mid - 1
}
}
return ans
}
func quickSort(arr []int, left int, right int) {
var i = left
var j = right
var pivot = arr[(left + right) / 2]
for i <= j {
for arr[i] < pivot {
i = i + 1
}
for arr[j] > pivot {
j = j - 1
}
if i <= j {
var temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
i = i + 1
j = j - 1
}
}
if left < j {
quickSort(arr, left, j)
}
if i < right {
quickSort(arr, i, right)
}
}
func printLoveImage() {
var n = 10
for i := 0; i < n; i = i + 1 {
for j := 0; j < n; j = j + 1 {
if (i == 0 && j % 3 != 0) || (i == 1 && j % 3 == 0) || (i - j == 2) || (i + j == 8) {
cprint("*")
} else {
cprint(" ")
}
}
cprint("\n")
}
}
```
- [ ] 支持求余运算,现在解析已经完成 但是没有生成对应汇编
```
var t = 6%3
```
- [ ] 支持位运算 现在解析已经完成 但是没有生成对应汇编
```
var a = 5&3
var b = 3|4
var c = ~6
```
- [x] 支持if then else语法,解析已完成,没有生成汇编,比较有挑战
```
if 3==5 {
cprint(3)
} else {
cprint(5)
}
```
- [ ] 完善函数调用
```
func fibonacci(n int) int {
if n == 0 {
return 0
}
if n == 1 {
return 1
}
return fibonacci(n-1) + fibonacci(n-2)
}
```
- [ ] 新增bool/string类型支持,现在只有int
```
if true {
cprint("true")
} else {
cprint("false")
}
```
- [ ] 支持数组类型,比较有挑战
```
var p = {3,4,5,5,3}
```
- [ ] 支持等于和不等于运算符
```
var p = 3==4
var q = 3!=4
```
- [ ] 支持三目运算
```
var p = 3==4?3:4
```
- [x] 调试工具,graphviz画出CFG
- [ ] 垃圾回收
- [ ] Runtime Inititalization
# Getting Started
> **GCC toolchain is required** to build the project, because we rely on its assembler and linker.
```bash
$ go build # build compiler
$ ./sprite test/program.y # compile the program
$ ./program # running it!
```
# Internals
For those who are interested in the internals of our compiler, here is a brief
overview of the pipeline:
```
Source Code -> Lexer -> Parser -> Compiler -> Assembler -> Executable
| | | | |
lexical | | | |
tokens | | | |
AST | | |
HIR+LIR | |
assembly |
code |
x86 binary
```
The HIR looks likes this:
Want to contribute to this project? Join our QQ group 285962642 and we will be happy to
help you get started.
# Credits
Thank you all for contributing to this project! See full list of contributors at
[AUTHORS](AUTHORS.md)