2008-07-18 18:18:29 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package AST
|
|
|
|
|
|
|
|
import Globals "globals"
|
|
|
|
import Universe "universe"
|
|
|
|
|
|
|
|
|
2008-07-23 17:04:11 -06:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Expressions
|
|
|
|
|
2008-08-05 19:52:37 -06:00
|
|
|
|
|
|
|
export type Literal struct {
|
2008-08-06 18:26:46 -06:00
|
|
|
pos_ int;
|
2008-08-05 19:52:37 -06:00
|
|
|
typ_ *Globals.Type;
|
|
|
|
b bool;
|
|
|
|
i int;
|
|
|
|
f float;
|
|
|
|
s string;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-06 18:26:46 -06:00
|
|
|
func (x *Literal) pos() int {
|
|
|
|
return x.pos_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-05 19:52:37 -06:00
|
|
|
func (x *Literal) typ() *Globals.Type {
|
|
|
|
return x.typ_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-06 18:26:46 -06:00
|
|
|
export func NewLiteral(pos int, typ *Globals.Type) *Literal {
|
2008-12-19 04:05:37 -07:00
|
|
|
x := new(*Literal);
|
2008-08-06 18:26:46 -06:00
|
|
|
x.pos_ = pos;
|
2008-08-05 19:52:37 -06:00
|
|
|
x.typ_ = typ;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export var Bad, True, False, Nil *Literal;
|
|
|
|
|
|
|
|
|
|
|
|
// NOTE We could use Globals.Object directly if we'd added a typ()
|
|
|
|
// method to its interface. However, this would require renaming the
|
|
|
|
// typ field everywhere... - Need to think about accessors again.
|
|
|
|
export type Object struct {
|
2008-08-06 18:26:46 -06:00
|
|
|
pos_ int;
|
2008-08-05 19:52:37 -06:00
|
|
|
obj *Globals.Object;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-06 18:26:46 -06:00
|
|
|
func (x *Object) pos() int {
|
|
|
|
return x.pos_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-05 19:52:37 -06:00
|
|
|
func (x *Object) typ() *Globals.Type {
|
|
|
|
return x.obj.typ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-06 18:26:46 -06:00
|
|
|
export func NewObject(pos int, obj* Globals.Object) *Object {
|
2008-12-19 04:05:37 -07:00
|
|
|
x := new(*Object);
|
2008-08-06 18:26:46 -06:00
|
|
|
x.pos_ = pos;
|
2008-08-05 19:52:37 -06:00
|
|
|
x.obj = obj;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-06 18:26:46 -06:00
|
|
|
// TODO model Selector as binary operation?
|
2008-08-05 19:52:37 -06:00
|
|
|
export type Selector struct {
|
2008-08-06 18:26:46 -06:00
|
|
|
pos_ int;
|
2008-08-05 19:52:37 -06:00
|
|
|
typ_ *Globals.Type;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-06 18:26:46 -06:00
|
|
|
func (x *Selector) pos() int {
|
|
|
|
return x.pos_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-05 19:52:37 -06:00
|
|
|
func (x *Selector) typ() *Globals.Type {
|
|
|
|
return x.typ_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-06 18:26:46 -06:00
|
|
|
export func NewSelector(pos int, typ *Globals.Type) *Selector {
|
2008-12-19 04:05:37 -07:00
|
|
|
x := new(*Selector);
|
2008-08-06 18:26:46 -06:00
|
|
|
x.pos_ = pos;
|
|
|
|
x.typ_ = typ;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-04 16:37:47 -06:00
|
|
|
export type BinaryExpr struct {
|
2008-08-06 18:26:46 -06:00
|
|
|
pos_ int;
|
2008-07-31 14:35:30 -06:00
|
|
|
typ_ *Globals.Type;
|
2008-07-18 18:18:29 -06:00
|
|
|
op int;
|
2008-07-31 14:35:30 -06:00
|
|
|
x, y Globals.Expr;
|
2008-07-18 18:18:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-06 18:26:46 -06:00
|
|
|
func (x *BinaryExpr) pos() int {
|
|
|
|
return x.pos_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-31 14:35:30 -06:00
|
|
|
func (x *BinaryExpr) typ() *Globals.Type {
|
|
|
|
return x.typ_;
|
2008-07-23 17:04:11 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-31 14:35:30 -06:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Statements
|
|
|
|
|
2008-08-04 16:37:47 -06:00
|
|
|
export type Block struct {
|
2008-07-23 17:04:11 -06:00
|
|
|
// TODO fill in
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-04 16:37:47 -06:00
|
|
|
export type IfStat struct {
|
2008-07-31 14:35:30 -06:00
|
|
|
cond Globals.Expr;
|
|
|
|
then_ Globals.Stat;
|
|
|
|
else_ Globals.Stat;
|
2008-07-18 18:18:29 -06:00
|
|
|
}
|
2008-08-05 19:52:37 -06:00
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Initialization
|
|
|
|
|
|
|
|
func init() {
|
2008-08-06 18:26:46 -06:00
|
|
|
Bad = NewLiteral(-1, Universe.bad_t);
|
|
|
|
True = NewLiteral(-1, Universe.bool_t); True.b = true;
|
|
|
|
False = NewLiteral(-1, Universe.bool_t); False.b = false;
|
|
|
|
Nil = NewLiteral(-1, Universe.nil_t);
|
2008-08-05 19:52:37 -06:00
|
|
|
}
|