zlang

package module
v0.0.0-...-acde71f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 21, 2014 License: MIT Imports: 7 Imported by: 0

README

Zlang

Implementation of a basic C-like Language

Example:

	/*
		1st Hello world script in zlang
	*/

	// extern keyword to allow binding of C functions
	// C equivalent: extern void puts(char*)
	extern puts(string)
	extern putchar(int)

	// pointer support
	// C equivalent: extern void* foo(int*)
	extern foo(@int) @

	// int square(int x) {
	square is func(int x) int {
		return x*x
	}

	// int main() {
	main is func() int {
		puts("Hello, 世界") // support UTF-8 encoding

		// Dynamic typing:
		// int result = square(2)
		result is square(2)

		// if (result != 4)
		if result neq 4 {
			puts("FAILURE: \"not 4\"")
		} else {
			puts("SUCCESS")
		}

		// result is already declared as an int
		// result = 32
		result is 32

		// not readable, but working
		c is 32 while c neq 127 { putchar(c) if c eq 126 {puts("")} c is c+1}

		return result
	}

Running:


# with compiler
$ zlc -o hello hello_world.zl 
$ ./hello
Hello, 世界
SUCCESS
 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
$ echo $?
32


# with interpreter
# TODO


Debugging Bytecode

# Dump bytecode
$ zlc --emit-llvm hello_world.zl 
	; ModuleID = 'hello_world.zl'

	@.str = private unnamed_addr constant [20 x i8] c"Hello World, \E4\B8\96\E7\95\8C\00", align 1
	@.str1 = private unnamed_addr constant [17 x i8] c"FAILURE: \22not 4\22\00", align 1
	@.str2 = private unnamed_addr constant [8 x i8] c"SUCCESS\00", align 1

	declare void @println(i8*)
	declare void* @foo(i32*)

	define i32 @square(i32 %x) nounwind uwtable {
	  %1 = alloca i32, align 4
	  store i32 %x, i32* %1, align 4
	  %2 = load i32* %1, align 4
	  %3 = load i32* %1, align 4
	  %4 = mul nsw i32 %2, %3
	  ret i32 %4
	}

	define i32 @main() nounwind uwtable {
	  %1 = alloca i32, align 4
	  %result = alloca i32, align 4
	  store i32 0, i32* %1
	  call void @println(i8* getelementptr inbounds ([20 x i8]* @.str, i32 0, i32 0))
	  %2 = call i32 @square(i32 2)
	  store i32 %2, i32* %result, align 4
	  %3 = load i32* %result, align 4
	  %4 = icmp eq i32 %3, 4
	  br i1 %4, label %5, label %6

	; <label>:5                                       ; preds = %0
	  call void @println(i8* getelementptr inbounds ([17 x i8]* @.str1, i32 0, i32 0))
	  br label %7

	; <label>:6                                       ; preds = %0
	  call void @println(i8* getelementptr inbounds ([8 x i8]* @.str2, i32 0, i32 0))
	  br label %7

	; <label>:7                                       ; preds = %6, %5
	  ret i32 0
	}

Status

Zlang is at early alpha stage and is for learning purposes only (don't implement real-world application with Zlang)

  • Lexer
  • Parser
  • JIT Interpreter (In progress)
  • Compiler
  • Compiler with optimizer pass

Language features:

  • Functions
  • Extern functions (C bindings)
  • Variable assignation
  • Mutable Variables
  • [-] Pointers (To be tested)
  • Conditions (if/else)
  • Loops (while)
  • Bitwise operations (and, or, xor, nand, nor, lshift, rshift)

Types:

  • Boolean (bool)
  • String (string)
  • Byte (byte)
  • Integer (int32, int64)
  • Float (float32, float64)
  • Arrays
  • Structs (In progress)

Documentation

Overview

Copyright 2013 Benjamin Gentil. All rights reserved. license can be found in the LICENSE file (MIT License)

Copyright 2013 Benjamin Gentil. All rights reserved. license can be found in the LICENSE file (MIT License)

Copyright 2013 Benjamin Gentil. All rights reserved. license can be found in the LICENSE file (MIT License)

Copyright 2013 Benjamin Gentil. All rights reserved. license can be found in the LICENSE file (MIT License)

Copyright 2013 Benjamin Gentil. All rights reserved. license can be found in the LICENSE file (MIT License)

Copyright 2013 Benjamin Gentil. All rights reserved. license can be found in the LICENSE file (MIT License)

Copyright 2013 Benjamin Gentil. All rights reserved. license can be found in the LICENSE file (MIT License)

Copyright 2013 Benjamin Gentil. All rights reserved. license can be found in the LICENSE file (MIT License)

Index

Constants

View Source
const (
	LeftBlockDelim    = '{'
	RightBlockDelim   = '}'
	LeftParentDelim   = '('
	RightParentDelim  = ')'
	ParamDelim        = ','
	StringDelim       = '"'
	EscapeCar         = '\\'
	Comment           = "//"
	LeftBlockComment  = "/*"
	RightBlockComment = "*/"
)
View Source
const (
	POINTER_CHAR = '@'
	TYPE_BOOL    = "bool"
	TYPE_BYTE    = "byte"
	TYPE_INT     = "int"
	TYPE_INT32   = "int32"
	TYPE_INT64   = "int64"
	TYPE_FLOAT   = "float"
	TYPE_FLOAT32 = "float32"
	TYPE_FLOAT64 = "float64"
	TYPE_STRING  = "string"
	TYPE_STRUCT  = "struct"
)
View Source
const (
	ResetCode  = "\033[0m" // reset color
	RedCode    = "\033[31m"
	GreenCode  = "\033[32m"
	YellowCode = "\033[33m"
)

Variables

View Source
var DEBUG = false

Functions

func Debug

func Debug(f string, v ...interface{})

func DebugDump

func DebugDump(v *llvm.Value)

func DebugDumpMod

func DebugDumpMod(m *llvm.Module)

func DisableColor

func DisableColor()

func DisableDebug

func DisableDebug()

func EnableColor

func EnableColor()

func EnableDebug

func EnableDebug()

func Error

func Error(f string, v ...interface{})

func Errorln

func Errorln(f string, v ...interface{})

func JNil

func JNil(s string) string

replace <nil> by null to have valid JSON fix also array prepresentation by separating object with , instead of space

func LLVMType

func LLVMType(name string) llvm.Type

func RegisterType

func RegisterType(name string, t llvm.Type)

func ZlangType

func ZlangType(t llvm.Type) string

TODO: return zlang type instead of llvm type

Types

type ContextValue

type ContextValue struct {
	FunctionName string
	VariableName string
}

type LexItem

type LexItem struct {
	Token Token
	Val   string
}

func (LexItem) String

func (i LexItem) String() string

type Lexer

type Lexer struct {
	// contains filtered or unexported fields
}

lexer holds the state of the scanner.

func NewLexer

func NewLexer(name, input string) *Lexer

Initialize the lexer

func (*Lexer) NextItem

func (l *Lexer) NextItem() LexItem

Parser will call NextItem until item's token is TOK_EOF or TOK_ERROR

type Node

type Node interface {
	CodeGen(*llvm.Module, *llvm.Builder) (*llvm.Value, error)
}

type NodeArray

type NodeArray struct {
	Node
	NodeExpr
	Values []NodeExpr
}

******************************************** Expressions ********************************************

func NArray

func NArray(val []NodeExpr) *NodeArray

func (*NodeArray) CodeGen

func (n *NodeArray) CodeGen(mod *llvm.Module, builder *llvm.Builder) (*llvm.Value, error)

func (*NodeArray) String

func (n *NodeArray) String() string

type NodeAssignement

type NodeAssignement struct {
	Node
	NodeExpr
	LHS *NodeIdentifier
	RHS NodeExpr
}

******************************************** Expressions ********************************************

func NAssignement

func NAssignement(lhs *NodeIdentifier, rhs NodeExpr) *NodeAssignement

func (*NodeAssignement) CodeGen

func (n *NodeAssignement) CodeGen(*llvm.Module, *llvm.Builder) (*llvm.Value, error)

func (*NodeAssignement) String

func (n *NodeAssignement) String() string

type NodeBinOperator

type NodeBinOperator struct {
	Node
	NodeExpr
	Operator string
	LHS      NodeExpr
	RHS      NodeExpr
}

******************************************** Expressions ********************************************

func NBinOp

func NBinOp(operator string, lhs, rhs NodeExpr) *NodeBinOperator

func (*NodeBinOperator) CodeGen

func (n *NodeBinOperator) CodeGen(mod *llvm.Module, builder *llvm.Builder) (*llvm.Value, error)

func (*NodeBinOperator) String

func (n *NodeBinOperator) String() string

type NodeBlock

type NodeBlock struct {
	Node
	NodeExpr
	Statements []NodeStmt
	Depth      int
}

******************************************** Expressions ********************************************

func NBlock

func NBlock(stmts []NodeStmt, depth int) *NodeBlock

func (*NodeBlock) CodeGen

func (n *NodeBlock) CodeGen(mod *llvm.Module, builder *llvm.Builder) (*llvm.Value, error)

func (*NodeBlock) String

func (n *NodeBlock) String() string

type NodeBool

type NodeBool struct {
	Node
	NodeExpr
	Value bool
}

******************************************** Expressions ********************************************

func NBool

func NBool(value string) *NodeBool

func (*NodeBool) CodeGen

func (n *NodeBool) CodeGen(*llvm.Module, *llvm.Builder) (*llvm.Value, error)

******************************************** Code generation ********************************************

func (*NodeBool) String

func (n *NodeBool) String() string

Make nodes printable

type NodeBreak

type NodeBreak struct {
	Node
	NodeStmt
}

******************************************** Statements ********************************************

func NBreak

func NBreak() *NodeBreak

func (*NodeBreak) CodeGen

func (n *NodeBreak) CodeGen(mod *llvm.Module, builder *llvm.Builder) (*llvm.Value, error)

func (*NodeBreak) String

func (n *NodeBreak) String() string

type NodeByte

type NodeByte struct {
	Node
	NodeExpr
	Value uint8
}

******************************************** Expressions ********************************************

func NByte

func NByte(value string) *NodeByte

func (*NodeByte) CodeGen

func (n *NodeByte) CodeGen(*llvm.Module, *llvm.Builder) (*llvm.Value, error)

func (*NodeByte) String

func (n *NodeByte) String() string

type NodeCall

type NodeCall struct {
	Node
	NodeExpr
	Name *NodeIdentifier
	Args []NodeExpr
}

******************************************** Expressions ********************************************

func NCall

func NCall(name *NodeIdentifier, args []NodeExpr) *NodeCall

func (*NodeCall) CodeGen

func (n *NodeCall) CodeGen(mod *llvm.Module, builder *llvm.Builder) (*llvm.Value, error)

func (*NodeCall) String

func (n *NodeCall) String() string

type NodeExpr

type NodeExpr interface {
	Node
	// contains filtered or unexported methods
}

******************************************** Expressions ********************************************

type NodeExpression

type NodeExpression struct {
	Node
	NodeStmt
	Expression NodeExpr
}

******************************************** Statements ********************************************

func NExpression

func NExpression(exp NodeExpr) *NodeExpression

func (*NodeExpression) CodeGen

func (n *NodeExpression) CodeGen(mod *llvm.Module, builder *llvm.Builder) (*llvm.Value, error)

func (*NodeExpression) String

func (n *NodeExpression) String() string

type NodeFloat

type NodeFloat struct {
	Node
	NodeExpr
	Value float32
}

******************************************** Expressions ********************************************

func NFloat

func NFloat(value string) *NodeFloat

func (*NodeFloat) CodeGen

func (n *NodeFloat) CodeGen(*llvm.Module, *llvm.Builder) (*llvm.Value, error)

func (*NodeFloat) String

func (n *NodeFloat) String() string

type NodeFunction

type NodeFunction struct {
	Node
	NodeStmt
	Proto *NodePrototype
	Body  *NodeBlock
}

******************************************** Statements ********************************************

func NFunction

func NFunction(proto *NodePrototype, body *NodeBlock) *NodeFunction

func (*NodeFunction) CodeGen

func (n *NodeFunction) CodeGen(mod *llvm.Module, builder *llvm.Builder) (*llvm.Value, error)

func (*NodeFunction) String

func (n *NodeFunction) String() string

type NodeIdentifier

type NodeIdentifier struct {
	Node
	NodeExpr
	Keys  []NodeExpr // used in array
	Value string
}

******************************************** Expressions ********************************************

func NIdentifier

func NIdentifier(value string, keys []NodeExpr) *NodeIdentifier

func (*NodeIdentifier) CodeGen

func (n *NodeIdentifier) CodeGen(mod *llvm.Module, builder *llvm.Builder) (*llvm.Value, error)

func (*NodeIdentifier) String

func (n *NodeIdentifier) String() string

type NodeIf

type NodeIf struct {
	Node
	NodeStmt
	Condition NodeExpr
	Body      *NodeBlock
	Elif      []*NodeBlock // TODO: support for else if
	Else      *NodeBlock
}

******************************************** Statements ********************************************

func NIf

func NIf(cond NodeExpr, body *NodeBlock, elif []*NodeBlock, els *NodeBlock) *NodeIf

func (*NodeIf) CodeGen

func (n *NodeIf) CodeGen(mod *llvm.Module, builder *llvm.Builder) (*llvm.Value, error)

func (*NodeIf) String

func (n *NodeIf) String() string

type NodeInteger

type NodeInteger struct {
	Node
	NodeExpr
	Value int
}

******************************************** Expressions ********************************************

func NInteger

func NInteger(value string) *NodeInteger

func (*NodeInteger) CodeGen

func (n *NodeInteger) CodeGen(*llvm.Module, *llvm.Builder) (*llvm.Value, error)

func (*NodeInteger) String

func (n *NodeInteger) String() string

type NodePrototype

type NodePrototype struct {
	Node
	NodeStmt
	Type *NodeIdentifier
	Name *NodeIdentifier
	Args []*NodeVariable
}

******************************************** Statements ********************************************

func NPrototype

func NPrototype(name, typ *NodeIdentifier, args []*NodeVariable) *NodePrototype

Make nodes creation easier

func (*NodePrototype) CodeGen

func (n *NodePrototype) CodeGen(mod *llvm.Module, builder *llvm.Builder) (*llvm.Value, error)

func (*NodePrototype) String

func (n *NodePrototype) String() string

type NodeReturn

type NodeReturn struct {
	Node
	NodeStmt
	Value NodeExpr
}

******************************************** Statements ********************************************

func NReturn

func NReturn(exp NodeExpr) *NodeReturn

func (*NodeReturn) CodeGen

func (n *NodeReturn) CodeGen(mod *llvm.Module, builder *llvm.Builder) (*llvm.Value, error)

func (*NodeReturn) String

func (n *NodeReturn) String() string

type NodeStmt

type NodeStmt interface {
	Node

	String() string
	// contains filtered or unexported methods
}

******************************************** Statements ********************************************

type NodeString

type NodeString struct {
	Node
	NodeExpr
	Value string
}

******************************************** Expressions ********************************************

func NString

func NString(value string) *NodeString

func (*NodeString) CodeGen

func (n *NodeString) CodeGen(mod *llvm.Module, builder *llvm.Builder) (*llvm.Value, error)

func (*NodeString) String

func (n *NodeString) String() string

type NodeVariable

type NodeVariable struct {
	Node
	NodeStmt
	Type       *NodeIdentifier
	Name       *NodeIdentifier
	AssignExpr *NodeAssignement
}

******************************************** Statements ********************************************

func NVariable

func NVariable(name, typ *NodeIdentifier, assign *NodeAssignement) *NodeVariable

func (*NodeVariable) CodeGen

func (n *NodeVariable) CodeGen(mod *llvm.Module, builder *llvm.Builder) (*llvm.Value, error)

func (*NodeVariable) String

func (n *NodeVariable) String() string

type NodeWhile

type NodeWhile struct {
	Node
	NodeStmt
	Condition NodeExpr
	Body      *NodeBlock
}

******************************************** Statements ********************************************

func NWhile

func NWhile(cond NodeExpr, body *NodeBlock) *NodeWhile

func (*NodeWhile) CodeGen

func (n *NodeWhile) CodeGen(mod *llvm.Module, builder *llvm.Builder) (*llvm.Value, error)

func (*NodeWhile) String

func (n *NodeWhile) String() string

type Parser

type Parser struct {
	Input   string
	Module  llvm.Module
	Builder llvm.Builder
	// contains filtered or unexported fields
}

func NewParser

func NewParser(name, input string) *Parser

func (*Parser) DepthIndent

func (p *Parser) DepthIndent() string

func (*Parser) NextItem

func (p *Parser) NextItem()

func (*Parser) Parse

func (p *Parser) Parse() (*NodeBlock, error)

func (*Parser) Pos

func (p *Parser) Pos() string

func (*Parser) RaiseError

func (p *Parser) RaiseError(f string, v ...interface{})

type Position

type Position struct {
	// contains filtered or unexported fields
}

For error reporting

type Token

type Token int
const (
	TOK_ERROR   Token = iota
	TOK_COMMENT       // /* comment */ or // comment
	TOK_BLANK         // space, tab
	TOK_ENDL          // \r, \n
	TOK_IDENTIFIER
	TOK_EOF

	TOK_BOOL  // true / false
	TOK_FLOAT // 1.0
	TOK_INT   // 1
	//TOK_CHAR   // 'a'
	TOK_BYTE   // 0x20
	TOK_STRING // "azerty"

	TOK_ASSIGN_S // is (=)
	TOK_NOT_S    // not (!)
	TOK_EQUAL_S  // eq (==)
	TOK_NEQ_S    // neq (!=)
	TOK_LT_S     // lt (<)
	TOK_LE_S     // le (<=)
	TOK_GT_S     // gt (>)
	TOK_GE_S     // ge (>=)

	TOK_AND_S  // and
	TOK_OR_S   // or
	TOK_NAND_S // nand
	TOK_NOR_S  // nor
	TOK_XOR_S  // xor
	TOK_LSH_S  // lshift
	TOK_RSH_S  // rshift

	TOK_PLUS  // +
	TOK_MINUS // -
	TOK_MUL   // *
	TOK_DIV   // /

	TOK_INC // ++
	TOK_DEC // --

	TOK_LPAREN   // (
	TOK_RPAREN   // )
	TOK_LBLOCK   // {
	TOK_RBLOCK   // }
	TOK_LBRACKET // [
	TOK_RBRACKET // ]
	TOK_DOT      // .
	TOK_COMMA    // ,

	TOK_FUNC   // function declaration
	TOK_EXTERN // extern (C bindings)
	TOK_RETURN // return
	TOK_IF     // if
	TOK_ELSE   // else
	TOK_SWITCH // switch
	TOK_CASE   // case
	TOK_BREAK  // break
	TOK_FOR    // for
	TOK_WHILE  // while

)

func (Token) IsOperator

func (t Token) IsOperator() bool

func (Token) Precedence

func (t Token) Precedence() int

func (Token) String

func (tok Token) String() string

Get a printable token

Directories

Path Synopsis
cmd
zlc

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL