Sophia
所属分类:数学计算
开发工具:GO
文件大小:0KB
下载次数:0
上传日期:2023-07-31 07:00:01
上 传 者:
sh-1993
说明: 我采用了一种类似于口齿不清的编程语言。
(My take on a small lisp like programming language.)
文件列表:
LICENSE (1063, 2023-12-20)
core/ (0, 2023-12-20)
core/alloc/ (0, 2023-12-20)
core/alloc/allocator.go (631, 2023-12-20)
core/builtin/ (0, 2023-12-20)
core/builtin/assert.go (770, 2023-12-20)
core/builtin/builtin.go (670, 2023-12-20)
core/builtin/filter.go (1563, 2023-12-20)
core/builtin/len.go (644, 2023-12-20)
core/builtin/len_test.go (607, 2023-12-20)
core/builtin/map.go (1364, 2023-12-20)
core/builtin/map_test.go (26, 2023-12-20)
core/builtin/println.go (396, 2023-12-20)
core/conf.go (1124, 2023-12-20)
core/consts/ (0, 2023-12-20)
core/consts/funcs.go (134, 2023-12-20)
core/consts/symbols.go (167, 2023-12-20)
core/debug/ (0, 2023-12-20)
core/debug/log.go (613, 2023-12-20)
core/debug/token.go (466, 2023-12-20)
core/eval/ (0, 2023-12-20)
core/eval/eval.go (288, 2023-12-20)
core/eval/eval_test.go (8283, 2023-12-20)
core/expr/ (0, 2023-12-20)
core/expr/add.go (729, 2023-12-20)
core/expr/and.go (644, 2023-12-20)
core/expr/any.go (305, 2023-12-20)
core/expr/array.go (539, 2023-12-20)
core/expr/bool.go (349, 2023-12-20)
core/expr/call.go (2529, 2023-12-20)
core/expr/div.go (728, 2023-12-20)
core/expr/equal.go (666, 2023-12-20)
core/expr/float.go (342, 2023-12-20)
core/expr/for.go (1445, 2023-12-20)
core/expr/func.go (495, 2023-12-20)
... ...
# Sophia
My take on a small lisp like programming language named after my girlfriend.
View the docs containing an overview, an in depth overview and a lot of
Examples [here](https://xnacly.github.io/Sophia/)
```lisp
(fun
square
(_ n)
(* n n)
)
(let n 12)
(let res (square n))
(put '{n}*{n} is {res}')
;; 12*12 is 144
```
## Try
### Running
```bash
git clone https://github.com/xnacly/sophia
go build
```
With a file:
```text
$ sophia ./examples/helloworld.phia
Hello World!
```
With an expression:
```
$ sophia -exp '(put "Hello World")'
Hello World!
```
```
$ echo '(put "Hello World")' | sophia
Hello World!
```
As a repl:
```
$ sophia
██████ █████ ██▓███ ██ ██ ██▓ ▄▄▄
██ ██ ██▓██ ██▓██ ██▓██████▄
▓██▄ ██ ██▓██ ██▓████████ █▄
████ ████▄█▓ ▓█ ██ ████▄▄▄▄██
██████ ████▓██ ▓███▓██ ▓█ ▓██
▓ ▓ ▓ ▓█
Welcome to the Sophia programming language repl - press or to quit...
sophia> (let name "user")
= [user]
sophia> (put 'Hello World, {name}!')
Hello World, user!
= []
sophia>
```
### Compiling
By default sophia executes input with its tree walk interpreter.
#### Targets
Currently the following targets are supported:
- [x] Javascript (all language features supported)
- [ ] Python (planned)
Specify the desired compilation target from the list above using the
`-target`-flag when invoking sophia with a source expression:
```text
$ sophia -exp='(put "hello world")' -target=js
console.log("Hello World")
```
#### Compiling sophia to javascript
Sophia produces valid minified javascript, for example compiling the example
from the beginning or a more complex example:
```lisp
(fun
square
(_ n)
(* n n)
)
(let n 12)
(let res (square a))
(put '{n}*{n} is {res}')
;; 12*12 is 144
```
```js
function square(t) {
return t * t;
}
let a = 12;
let r = square(a);
console.log(`${a}*${a} is ${r}`);
```
```lisp
;; leetcode 327 Maximum Count of positive integer and negative integer
(let example1
(not 2) ;; negated: (not 2) -> -2
(not 1)
(not 1) 1 2 3)
(let example2 (not 3) (not 2) (not 1) 0 0 1 2)
(let example3 5 20 66 1314)
;; returns a if bigger than b, otherwise b
;; max: float, float -> float
(fun max (_ a b)
(let max) ;; created as nil
(match
(if (lt a b) (let max b))
(let max a))
max) ;; return without extra statement
;; counts negative and positve numbers in arr, returns the higher amount
;; solve: [float] -> float
(fun solve (_ arr)
(let pos 0)
(let neg 0)
(for (_ i) arr
(match
(if (lt i 0) (let pos (+ pos 1)))
(let neg (+ neg 1))))
(max neg pos))
(put (solve example1)) ;; 3
(put (solve example3)) ;; 4
(put (solve example2)) ;; 4
```
```js
let example1 = [-2, -1, -1, 1, 2, 3];
let example2 = [-3, -2, -1, 0, 0, 1, 2];
let example3 = [5, 20, 66, 1314];
function max(a, b) {
let max;
if (a < b) {
max = b;
} else {
max = a;
}
return max;
}
function solve(arr) {
let pos = 0;
let neg = 0;
for (let i of arr) {
if (i < 0) {
pos = pos + 1;
} else {
neg = neg + 1;
}
}
return max(neg, pos);
}
console.log(solve(example1));
console.log(solve(example3));
console.log(solve(example2));
```
近期下载者:
相关文件:
收藏者: