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,19 +54,19 @@ func (p *Program) String() string {
type Type string type Type string
type Parameter struct { type Argument struct {
Name string Name string
Type Type Type Type
} }
type FunctionDeclaration struct { type FunctionDeclaration struct {
Token token.Token // The token.FN Token token.Token // The token.FN
Body Expression Body Expression
Name string Name string
Parameters []Parameter Args []Argument
} }
func ArgsToString(args []Parameter) string { func ArgsToString(args []Argument) string {
var b strings.Builder var b strings.Builder
for _, arg := range args { for _, arg := range args {
@ -80,7 +80,7 @@ func (fd *FunctionDeclaration) declarationNode() {}
func (fd *FunctionDeclaration) TokenLiteral() string { return fd.Token.Literal } func (fd *FunctionDeclaration) TokenLiteral() string { return fd.Token.Literal }
func (fd *FunctionDeclaration) Tok() token.Token { return fd.Token } func (fd *FunctionDeclaration) Tok() token.Token { return fd.Token }
func (fd *FunctionDeclaration) String() string { 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 // 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 { func (ae *AssignmentExpression) String() string {
return fmt.Sprintf("%s = %s", ae.Lhs.String(), ae.Rhs.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 ## Function Calls
```tt ```tt
fn hi(arg1: i32, arg2: i32) = { fn hi(hello: i32) = {
arg1 + 3
// Hi
arg2
}; };
fn main() = { fn main() = {
hi!;
// Args hi();
hi;
arg1 := 2;
arg2 := 2;
//hi(arg1, arg2) |> hi(arg2);
//hi(arg1, arg2) |> hi(arg1, |);
}; };
``` ```

View File

@ -1,59 +1,53 @@
# ml2 ts=2 sts=2 sw=2 # ml2 ts=2 sts=2 sw=2
{ {
description = "A simple Go package"; description = "A simple Go package";
# Nixpkgs / NixOS version to use. # Nixpkgs / NixOS version to use.
inputs.nixpkgs.url = "nixpkgs/nixos-unstable"; inputs.nixpkgs.url = "nixpkgs/nixos-unstable";
outputs = { outputs = { self, nixpkgs }:
self, let
nixpkgs,
}: let
# to work with older version of flakes
lastModifiedDate = self.lastModifiedDate or self.lastModified or "19700101";
# Generate a user-friendly version number. # to work with older version of flakes
version = builtins.substring 0 8 lastModifiedDate; lastModifiedDate = self.lastModifiedDate or self.lastModified or "19700101";
# System types to support. # Generate a user-friendly version number.
supportedSystems = ["x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin"]; version = builtins.substring 0 8 lastModifiedDate;
# Helper function to generate an attrset '{ x86_64-linux = f "x86_64-linux"; ... }'. # System types to support.
forAllSystems = nixpkgs.lib.genAttrs supportedSystems; supportedSystems = [ "x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin" ];
# Helper function to generate an attrset '{ x86_64-linux = f "x86_64-linux"; ... }'.
forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
# Nixpkgs instantiated for supported system types.
nixpkgsFor = forAllSystems (system: import nixpkgs { inherit system; });
# 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
pkgs = nixpkgsFor.${system};
in { in {
tt = pkgs.callPackage ./nix/package.nix {version = version;}; # Provide some binary packages for selected system types.
}); packages = forAllSystems (system:
let
pkgs = nixpkgsFor.${system};
in
{
tt = pkgs.callPackage ./nix/package.nix { version = version; };
});
# Add dependencies that are only needed for development # Add dependencies that are only needed for development
devShells = forAllSystems (system: let devShells = forAllSystems (system:
pkgs = nixpkgsFor.${system}; let
in { pkgs = nixpkgsFor.${system};
default = pkgs.mkShell { in
buildInputs = with pkgs; [ {
go default = pkgs.mkShell {
gopls buildInputs = with pkgs; [ go gopls gotools go-tools qbe (if system == "x86_64-linux" then [fasm] else []) ];
gotools };
go-tools });
qbe
(
if system == "x86_64-linux"
then [fasm]
else []
)
];
};
});
# The default package for 'nix build'. This makes sense if the # The default package for 'nix build'. This makes sense if the
# flake provides only one package or there is a clear "main" # flake provides only one package or there is a clear "main"
# package. # package.
defaultPackage = forAllSystems (system: self.packages.${system}.tt); defaultPackage = forAllSystems (system: self.packages.${system}.tt);
}; };
} }

View File

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

View File

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

View File

@ -186,23 +186,23 @@ func (p *Parser) parseType() (t ast.Type, ok bool) {
return ast.Type(p.curToken.Literal), true return ast.Type(p.curToken.Literal), true
} }
func (p *Parser) parseParameterList() ([]ast.Parameter, bool) { func (p *Parser) parseArgumentList() ([]ast.Argument, bool) {
parameters := []ast.Parameter{} args := []ast.Argument{}
for p.peekTokenIs(token.Ident) { for p.peekTokenIs(token.Ident) {
p.nextToken() p.nextToken()
name := p.curToken.Literal name := p.curToken.Literal
if ok, _ := p.expectPeek(token.Colon); !ok { if ok, _ := p.expectPeek(token.Colon); !ok {
return parameters, false return args, false
} }
p.nextToken() p.nextToken()
t, ok := p.parseType() t, ok := p.parseType()
if !ok { 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) { if !p.peekTokenIs(token.Comma) {
break break
@ -210,7 +210,7 @@ func (p *Parser) parseParameterList() ([]ast.Parameter, bool) {
p.nextToken() p.nextToken()
} }
return parameters, true return args, true
} }
func (p *Parser) parseDeclaration() ast.Declaration { func (p *Parser) parseDeclaration() ast.Declaration {
@ -227,7 +227,7 @@ func (p *Parser) parseDeclaration() ast.Declaration {
return nil return nil
} }
params, ok := p.parseParameterList() args, ok := p.parseArgumentList()
if !ok { if !ok {
return nil return nil
@ -247,10 +247,10 @@ func (p *Parser) parseDeclaration() ast.Declaration {
} }
return &ast.FunctionDeclaration{ return &ast.FunctionDeclaration{
Token: tok, Token: tok,
Name: name, Name: name,
Body: expr, Body: expr,
Parameters: params, Args: args,
} }
} }
@ -390,8 +390,6 @@ func (p *Parser) parseVariable() ast.Expression {
switch p.peekToken.Type { switch p.peekToken.Type {
case token.Colon: case token.Colon:
return p.parseVariableDeclaration() return p.parseVariableDeclaration()
case token.OpenParen:
return p.parseFunctionCall()
default: default:
return &ast.VariableReference{ return &ast.VariableReference{
Token: p.curToken, Token: p.curToken,
@ -429,32 +427,6 @@ func (p *Parser) parseVariableDeclaration() ast.Expression {
return variable 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 // Binary
func (p *Parser) parseBinaryExpression(lhs ast.Expression) ast.Expression { func (p *Parser) parseBinaryExpression(lhs ast.Expression) ast.Expression {

View File

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

View File

@ -31,13 +31,13 @@ func (c *Checker) inferDeclaration(decl ast.Declaration) (tast.Declaration, erro
case *ast.FunctionDeclaration: case *ast.FunctionDeclaration:
vars := make(Variables) vars := make(Variables)
arguments := []tast.Argument{} arguments := []tast.Argument{}
for _, param := range decl.Parameters { for _, arg := range decl.Args {
t, ok := types.From(param.Type) t, ok := types.From(arg.Type)
if !ok { 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 vars[arg.Name] = t
arguments = append(arguments, tast.Argument{Name: param.Name, Type: t}) arguments = append(arguments, tast.Argument{Name: arg.Name, Type: t})
} }
body, err := c.inferExpression(vars, decl.Body) body, err := c.inferExpression(vars, decl.Body)
c.functionVariables[decl.Name] = vars 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)} s := Scope{Variables: make(map[string]Var)}
for i, param := range d.Parameters { for i, arg := range d.Args {
uniq := s.SetUniq(param.Name) uniq := s.SetUniq(arg.Name)
d.Parameters[i].Name = uniq d.Args[i].Name = uniq
} }
err := VarResolveExpr(&s, d.Body) err := VarResolveExpr(&s, d.Body)
functionToScope[d.Name] = s functionToScope[d.Name] = s