Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
JokeNeverSoke committed Dec 19, 2023
1 parent 4b812ae commit 235f41f
Show file tree
Hide file tree
Showing 7 changed files with 355 additions and 115 deletions.
50 changes: 46 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,55 @@
# TRack

T(yped)Rack(et) is an experimental in-progress type-checker/compiler I'm writing for
the racket language. Help is appreciated in _any_ of the following domains:
T(yped)Rack(et) is an experimental in-progress type-checker/compiler I'm writing
for the racket language. Help is appreciated in _any_ of the following domains:

- [ ] Compiler formatting
- [ ] _Any_ enhancement to the codebas
- [ ] _Any_ enhancement to the codebase
- [ ] Environment definitions (e.g. language packs & imported libraries)
- [ ] Test suites

## Usage

_unusable right now_

## Examples

Track compiles this

```racket
(define (f x : String & y : Number -> Number)
(local ([define (g z : Number -> Number) (+ y z)])
(+ (string->number x) (g y) 5)))
```

into this

```racket
#lang htdp/isl+
;; f : String Number -> Number
(define (f x y)
(local (
;; g : Number -> Number
(define (g z)
(+
y
z
)))
(+
(string->number
x
)
(g
y
)
5
)))
```

> _yes, i know its ugly. i'm trying to improve the compiler output. or you can
> just run the output over `raco fmt` again._
## Related

- [Racket-huh](https://github.com/jokeneversoke/racket-huh) Executes Racket code **in browser** within embeds.
- [Racket-huh](https://github.com/jokeneversoke/racket-huh) - Executes Racket
code **in browser** within embeds.
2 changes: 2 additions & 0 deletions cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ switch (command) {
const { typecheck } = await import("./typecheck.ts");
const source = await Deno.readTextFile(file);
const tokens = tokenize(source);

console.log(tokens);
const ast = parse(tokens);
const errors = typecheck(ast);
if (errors.length > 0) {
Expand Down
5 changes: 3 additions & 2 deletions compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ export function compile(ast: AST.Program, opts: CompileOptions = {
: typeToString(param.type as AST.TypeAnnotation);
}).join(" ")
} -> ${
expr.signature.type === "untyped-parameters"
expr.signature.type === "untyped-parameters" ||
expr.signature.returnType === null
? "any"
: typeToString(expr.signature.returnType)
}`,
Expand Down Expand Up @@ -167,7 +168,7 @@ export function compile(ast: AST.Program, opts: CompileOptions = {
function compileDefineStruct(expr: AST.DefineStruct): PartialCode {
return partial([
"(define-struct " + compileIdentifier(expr.name) +
" (" + compileFunctionSignature(expr.fields) + ")",
" (" + compileFunctionSignature(expr.fields) + "))",
]);
}
function compileCondStatement(expr: AST.CondStatement): PartialCode {
Expand Down
47 changes: 18 additions & 29 deletions compiled.rkt
Original file line number Diff line number Diff line change
@@ -1,30 +1,19 @@
#lang htdp/isl+
;; k : Number Number -> Number
(define (k x y)
"")
;; v : String -> Number
(define (v u)
(string->number
u
))
(+
(k
1
2
)
(v
""
)
)
(+
(v
1
)
(k
2
)
)
(k
3
""
)
;; f : String Number -> Number
(define (f x y)
(local (
;; g : Number -> Number
(define (g z)
(+
y
z
)))
(+
(string->number
x
)
(g
y
)
5
)))
12 changes: 3 additions & 9 deletions example.trkt
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@

(define (k x : Number & y : Number -> Number)
"")
(define (v u : String -> Number)
(string->number u))
(+ (k 1 2) (v "3"))
;; wrong example
(+ (v 1) (k 2))
(k 3 "4")
(define (f x : String & y : Number -> Number)
(local ([define (g z : Number -> Number) (+ y z)])
(+ (string->number x) (g y) 5)))
18 changes: 14 additions & 4 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,11 @@ export function tokenize(code: string) {
}

switch (char) {
case "[":
case "(":
pushToken({ type: "leftParen", isTyped: false, ...position() });
break;
case "]":
case ")":
pushToken({ type: "rightParen", isTyped: false, ...position() });
break;
Expand Down Expand Up @@ -509,7 +511,7 @@ export type TypedParameters = {
name: Identifier;
type: TypeAnnotation;
}[];
returnType: TypeAnnotation;
returnType: TypeAnnotation | null;
};
export type TypeAnnotation = TypeFunction | TypeLiteral;
export type TypeFunction = {
Expand Down Expand Up @@ -738,10 +740,13 @@ export function parse(tokens: Token[]) {
};
}
function defineStruct(): DefineStruct {
consume("leftParen", "Expected ( before define-struct");
consume("define-struct", "Expected define-struct");
const name = identifier();
consume("leftParen", "Expected ( before struct fields");
const fields = parameters();
consume("rightParen", "Expected ) after struct fields");
consume("rightParen", "Expected ) after define-struct");
return {
type: "define-struct",
name,
Expand All @@ -766,15 +771,20 @@ export function parse(tokens: Token[]) {
consume("t-is", "Expected :");
const type = typeAnnotation();
parameters.push({ name, type });
while (!check("t-produce")) {
while (check("t-and")) {
consume("t-and", "Expected &");
const name = identifier();
consume("t-is", "Expected :");
const type = typeAnnotation();
parameters.push({ name, type });
}
consume("t-produce", "Expected ->");
const returnType = typeAnnotation();

let returnType: TypeAnnotation | null = null;
if (check("t-produce")) {
consume("t-produce", "Expected ->");
returnType = typeAnnotation();
}

return {
type: "typed-parameters",
parameters,
Expand Down
Loading

0 comments on commit 235f41f

Please sign in to comment.