Compare commits

..

No commits in common. "19c8cb910329571cf401489e3faea5b29b37e6b5" and "e0f2da3174ed70e7bf5754d36f235b89b68cf746" have entirely different histories.

9 changed files with 73 additions and 148 deletions

View File

@ -54,7 +54,7 @@ func (p *Program) String() string {
type Type string
type Parameter struct {
type Argument struct {
Name string
Type Type
}
@ -63,10 +63,10 @@ type FunctionDeclaration struct {
Token token.Token // The token.FN
Body Expression
Name string
Parameters []Parameter
Args []Argument
}
func ArgsToString(args []Parameter) string {
func ArgsToString(args []Argument) string {
var b strings.Builder
for _, arg := range args {
@ -80,7 +80,7 @@ func (fd *FunctionDeclaration) declarationNode() {}
func (fd *FunctionDeclaration) TokenLiteral() string { return fd.Token.Literal }
func (fd *FunctionDeclaration) Tok() token.Token { return fd.Token }
func (fd *FunctionDeclaration) String() string {
return fmt.Sprintf("fn %v(%v) = %v;", fd.Name, ArgsToString(fd.Parameters), fd.Body.String())
return fmt.Sprintf("fn %v(%v) = %v;", fd.Name, ArgsToString(fd.Args), fd.Body.String())
}
// Represents a Expression that we failed to parse
@ -265,31 +265,3 @@ func (ae *AssignmentExpression) Tok() token.Token { return ae.Token }
func (ae *AssignmentExpression) String() string {
return fmt.Sprintf("%s = %s", ae.Lhs.String(), ae.Rhs.String())
}
// identifier ( expressions... )
type FunctionCall struct {
Token token.Token // The identifier
Identifier string
Arguments []Expression
}
func (fc *FunctionCall) expressionNode() {}
func (fc *FunctionCall) TokenLiteral() string { return fc.Token.Literal }
func (fc *FunctionCall) Tok() token.Token { return fc.Token }
func (fc *FunctionCall) String() string {
b := strings.Builder{}
b.WriteString(fc.Identifier)
b.WriteRune('(')
for i, arg := range fc.Arguments {
b.WriteString(arg.String())
if i < (len(fc.Arguments) - 1) {
b.WriteRune(',')
}
}
b.WriteRune(')')
return b.String()
}

View File

@ -5,21 +5,14 @@ Playground for language design dessisions
## Function Calls
```tt
fn hi(arg1: i32, arg2: i32) = {
arg1 +
// Hi
arg2
fn hi(hello: i32) = {
3
};
fn main() = {
// Args
arg1 := 2;
arg2 := 2;
//hi(arg1, arg2) |> hi(arg2);
//hi(arg1, arg2) |> hi(arg1, |);
hi!;
hi();
hi;
};
```

View File

@ -1,14 +1,14 @@
# ml2 ts=2 sts=2 sw=2
{
description = "A simple Go package";
# Nixpkgs / NixOS version to use.
inputs.nixpkgs.url = "nixpkgs/nixos-unstable";
outputs = {
self,
nixpkgs,
}: let
outputs = { self, nixpkgs }:
let
# to work with older version of flakes
lastModifiedDate = self.lastModifiedDate or self.lastModified or "19700101";
@ -23,31 +23,25 @@
# Nixpkgs instantiated for supported system types.
nixpkgsFor = forAllSystems (system: import nixpkgs { inherit system; });
in {
# Provide some binary packages for selected system types.
packages = forAllSystems (system: let
packages = forAllSystems (system:
let
pkgs = nixpkgsFor.${system};
in {
in
{
tt = pkgs.callPackage ./nix/package.nix { version = version; };
});
# Add dependencies that are only needed for development
devShells = forAllSystems (system: let
devShells = forAllSystems (system:
let
pkgs = nixpkgsFor.${system};
in {
in
{
default = pkgs.mkShell {
buildInputs = with pkgs; [
go
gopls
gotools
go-tools
qbe
(
if system == "x86_64-linux"
then [fasm]
else []
)
];
buildInputs = with pkgs; [ go gopls gotools go-tools qbe (if system == "x86_64-linux" then [fasm] else []) ];
};
});

View File

@ -1,3 +1,4 @@
# ml2 ts=2 sts=2 sw=2
with import <nixpkgs> {};
{version ? "HEAD"}: callPackage ./package.nix {inherit version;}

View File

@ -1,9 +1,5 @@
# ml2 ts=2 sts=2 sw=2
{
buildGoModule,
version ? "HEAD",
}:
buildGoModule {
{buildGoModule, version ? "HEAD"}: buildGoModule {
pname = "tt";
inherit version;

View File

@ -186,23 +186,23 @@ func (p *Parser) parseType() (t ast.Type, ok bool) {
return ast.Type(p.curToken.Literal), true
}
func (p *Parser) parseParameterList() ([]ast.Parameter, bool) {
parameters := []ast.Parameter{}
func (p *Parser) parseArgumentList() ([]ast.Argument, bool) {
args := []ast.Argument{}
for p.peekTokenIs(token.Ident) {
p.nextToken()
name := p.curToken.Literal
if ok, _ := p.expectPeek(token.Colon); !ok {
return parameters, false
return args, false
}
p.nextToken()
t, ok := p.parseType()
if !ok {
return parameters, false
return args, false
}
parameters = append(parameters, ast.Parameter{Type: t, Name: name})
args = append(args, ast.Argument{Type: t, Name: name})
if !p.peekTokenIs(token.Comma) {
break
@ -210,7 +210,7 @@ func (p *Parser) parseParameterList() ([]ast.Parameter, bool) {
p.nextToken()
}
return parameters, true
return args, true
}
func (p *Parser) parseDeclaration() ast.Declaration {
@ -227,7 +227,7 @@ func (p *Parser) parseDeclaration() ast.Declaration {
return nil
}
params, ok := p.parseParameterList()
args, ok := p.parseArgumentList()
if !ok {
return nil
@ -250,7 +250,7 @@ func (p *Parser) parseDeclaration() ast.Declaration {
Token: tok,
Name: name,
Body: expr,
Parameters: params,
Args: args,
}
}
@ -390,8 +390,6 @@ func (p *Parser) parseVariable() ast.Expression {
switch p.peekToken.Type {
case token.Colon:
return p.parseVariableDeclaration()
case token.OpenParen:
return p.parseFunctionCall()
default:
return &ast.VariableReference{
Token: p.curToken,
@ -429,32 +427,6 @@ func (p *Parser) parseVariableDeclaration() ast.Expression {
return variable
}
func (p *Parser) parseFunctionCall() ast.Expression {
if ok, errExpr := p.expect(token.Ident); !ok {
return errExpr
}
funcCall := &ast.FunctionCall{Token: p.curToken, Identifier: p.curToken.Literal}
if ok, errExpr := p.expectPeek(token.OpenParen); !ok {
return errExpr
}
args := []ast.Expression{}
for !p.peekTokenIs(token.CloseParen) {
p.nextToken()
args = append(args, p.parseExpression(PrecLowest))
}
// Move onto the ')'
p.nextToken()
funcCall.Arguments = args
return funcCall
}
// Binary
func (p *Parser) parseBinaryExpression(lhs ast.Expression) ast.Expression {

View File

@ -1,14 +1,11 @@
fn main() = {
i := 5;
if i == 5 {
0
} else {
1
};
test2(3)
}
};
fn test2(hello: i64) = {

View File

@ -31,13 +31,13 @@ func (c *Checker) inferDeclaration(decl ast.Declaration) (tast.Declaration, erro
case *ast.FunctionDeclaration:
vars := make(Variables)
arguments := []tast.Argument{}
for _, param := range decl.Parameters {
t, ok := types.From(param.Type)
for _, arg := range decl.Args {
t, ok := types.From(arg.Type)
if !ok {
return nil, c.error(decl.Token, "could not find the type %q for argument %q", param.Type, param.Name)
return nil, c.error(decl.Token, "could not find the type %q for argument %q", arg.Type, arg.Name)
}
vars[param.Name] = t
arguments = append(arguments, tast.Argument{Name: param.Name, Type: t})
vars[arg.Name] = t
arguments = append(arguments, tast.Argument{Name: arg.Name, Type: t})
}
body, err := c.inferExpression(vars, decl.Body)
c.functionVariables[decl.Name] = vars

View File

@ -79,9 +79,9 @@ func VarResolve(p *ast.Program) (map[string]Scope, error) {
}
s := Scope{Variables: make(map[string]Var)}
for i, param := range d.Parameters {
uniq := s.SetUniq(param.Name)
d.Parameters[i].Name = uniq
for i, arg := range d.Args {
uniq := s.SetUniq(arg.Name)
d.Args[i].Name = uniq
}
err := VarResolveExpr(&s, d.Body)
functionToScope[d.Name] = s