Skip to content
Snippets Groups Projects
Unverified Commit d42a6819 authored by Yorick Peterse's avatar Yorick Peterse
Browse files

Rename Expressions to Body

This is a bit shorter to type, and better signals what the AST node is
meant for (e.g. the body of an "object").
parent 486cc3c8
No related branches found
No related tags found
No related merge requests found
# AST types for closures and lambdas.
import std::compiler::ast::expressions::Expressions
import std::compiler::ast::body::Body
import std::compiler::ast::node::Node
import std::compiler::ast::type_parameter::TypeParameter
import std::compiler::source_location::SourceLocation
Loading
Loading
@@ -76,18 +76,18 @@ impl Node for Argument {
# A closure that was created without the `do` or `lambda` keyword.
object BasicClosure {
# The expressions in the block's body.
@body: Expressions
@body: Body
 
# The source location of the block.
@location: SourceLocation
 
def init(body: Expressions, location: SourceLocation) {
def init(body: Body, location: SourceLocation) {
@body = body
@location = location
}
 
# Returns the expressions in the block's body.
def body -> Expressions {
def body -> Body {
@body
}
}
Loading
Loading
@@ -107,7 +107,7 @@ object Closure {
@arguments: Array!(Argument)
 
# The expressions in the closure's body.
@body: Expressions
@body: Body
 
# The type of the value this closure might throw.
@throw_type: ?Node
Loading
Loading
@@ -121,7 +121,7 @@ object Closure {
def init(
type_parameters: Array!(TypeParameter),
arguments: Array!(Argument),
body: Expressions,
body: Body,
throw_type: ?Node,
return_type: ?Node,
location: SourceLocation
Loading
Loading
@@ -145,7 +145,7 @@ object Closure {
}
 
# Returns the expressions in the block's body.
def body -> Expressions {
def body -> Body {
@body
}
 
Loading
Loading
@@ -175,7 +175,7 @@ object Lambda {
@arguments: Array!(Argument)
 
# The expressions in the lambda's body.
@body: Expressions
@body: Body
 
# The type of the value this lambda might throw.
@throw_type: ?Node
Loading
Loading
@@ -189,7 +189,7 @@ object Lambda {
def init(
type_parameters: Array!(TypeParameter),
arguments: Array!(Argument),
body: Expressions,
body: Body,
throw_type: ?Node,
return_type: ?Node,
location: SourceLocation
Loading
Loading
@@ -213,7 +213,7 @@ object Lambda {
}
 
# Returns the expressions in the block's body.
def body -> Expressions {
def body -> Body {
@body
}
 
Loading
Loading
@@ -246,7 +246,7 @@ object MethodDefinition {
@arguments: Array!(Argument)
 
# The expressions in the method's body.
@body: Expressions
@body: Body
 
# The type of the value this method might throw.
@throw_type: ?Node
Loading
Loading
@@ -268,7 +268,7 @@ object MethodDefinition {
throw_type: ?Node,
return_type: ?Node,
static_method: Boolean,
body: Expressions,
body: Body,
location: SourceLocation
) {
@name = name
Loading
Loading
@@ -297,7 +297,7 @@ object MethodDefinition {
}
 
# Returns the expressions in the block's body.
def body -> Expressions {
def body -> Body {
@body
}
 
Loading
Loading
Loading
Loading
@@ -3,7 +3,7 @@ import std::compiler::ast::node::Node
import std::compiler::source_location::SourceLocation
 
# A collection of different expressions.
object Expressions {
object Body {
# The nodes stored in this collection of expressions.
@children: Array!(Node)
 
Loading
Loading
@@ -21,7 +21,7 @@ object Expressions {
}
}
 
impl Node for Expressions {
impl Node for Body {
def location -> SourceLocation {
@location
}
Loading
Loading
# AST types for control flow constructs.
import std::compiler::ast::expressions::Expressions
import std::compiler::ast::body::Body
import std::compiler::ast::node::Node
import std::compiler::source_location::SourceLocation
 
Loading
Loading
@@ -60,8 +60,8 @@ object Try {
# The name of the local variable to store the error in.
@error_variable: ?String
 
# The expression to run when an error is thrown.
@else_expression: Expressions
# The body to run when an error is thrown.
@else_body: Body
 
# The source location of the throw expression.
@location: SourceLocation
Loading
Loading
@@ -69,12 +69,12 @@ object Try {
def init(
expression: Node,
error_variable: ?String,
else_expression: Expressions,
else_body: Body,
location: SourceLocation
) {
@expression = expression
@error_variable = error_variable
@else_expression = else_expression
@else_body = else_body
@location = location
}
 
Loading
Loading
@@ -89,8 +89,8 @@ object Try {
}
 
# Returns the expression to run when an error is thrown.
def else_expression -> Expressions {
@else_expression
def else_body -> Body {
@else_body
}
}
 
Loading
Loading
# AST types for objects and traits.
import std::compiler::ast::expressions::Expressions
import std::compiler::ast::body::Body
import std::compiler::ast::node::Node
import std::compiler::ast::type_parameter::TypeParameter
import std::compiler::ast::variables::Constant
Loading
Loading
@@ -14,7 +14,7 @@ object ObjectDefinition {
@type_parameters: Array!(TypeParameter)
 
# The expressions inside the object body.
@body: Expressions
@body: Body
 
# The source location of the definition.
@location: SourceLocation
Loading
Loading
@@ -22,7 +22,7 @@ object ObjectDefinition {
def init(
name: Constant,
type_parameters: Array!(TypeParameter),
body: Expressions,
body: Body,
location: SourceLocation
) {
@name = name
Loading
Loading
@@ -42,7 +42,7 @@ object ObjectDefinition {
}
 
# Returns the expressions contained in this object definition.
def body -> Expressions {
def body -> Body {
@body
}
}
Loading
Loading
@@ -99,7 +99,7 @@ object TraitDefinition {
@required_traits: Array!(Constant)
 
# The expressions inside the trait's body.
@body: Expressions
@body: Body
 
# The source location of the trait definition.
@location: SourceLocation
Loading
Loading
@@ -108,7 +108,7 @@ object TraitDefinition {
name: Constant,
type_parameters: Array!(TypeParameter),
required_traits: Array!(Constant),
body: Expressions,
body: Body,
location: SourceLocation
) {
@name = name
Loading
Loading
@@ -134,7 +134,7 @@ object TraitDefinition {
}
 
# Returns the expressions contained in this object definition.
def body -> Expressions {
def body -> Body {
@body
}
}
Loading
Loading
@@ -158,7 +158,7 @@ object ImplementTrait {
@trait_bounds: Array!(TypeParameter)
 
# The expressions contained in the body of the implementation.
@body: Expressions
@body: Body
 
# The source location of the implementation.
@location: SourceLocation
Loading
Loading
@@ -167,7 +167,7 @@ object ImplementTrait {
trait_name: Constant,
object_name: Constant,
trait_bounds: Array!(TypeParameter),
body: Expressions,
body: Body,
location: SourceLocation
) {
@trait_name = trait_name
Loading
Loading
@@ -193,7 +193,7 @@ object ImplementTrait {
}
 
# Returns the expressions contained in this implementation.
def body -> Expressions {
def body -> Body {
@body
}
}
Loading
Loading
@@ -210,16 +210,12 @@ object ReopenObject {
@name: Constant
 
# The expressions contained in the body of the object.
@body: Expressions
@body: Body
 
# The source location of the implementation.
@location: SourceLocation
 
def init(
name: Constant,
body: Expressions,
location: SourceLocation
) {
def init(name: Constant, body: Body, location: SourceLocation) {
@name = name
@body = body
@location = location
Loading
Loading
@@ -231,7 +227,7 @@ object ReopenObject {
}
 
# Returns the body of the `impl` expression.
def body -> Expressions {
def body -> Body {
@body
}
}
Loading
Loading
Loading
Loading
@@ -6,7 +6,7 @@ import std::byte_array::ToByteArray
import std::compiler::ast::blocks::*
import std::compiler::ast::comments::*
import std::compiler::ast::control_flow::*
import std::compiler::ast::expressions::Expressions
import std::compiler::ast::body::Body
import std::compiler::ast::imports::*
import std::compiler::ast::literals::*
import std::compiler::ast::node::Node
Loading
Loading
@@ -82,7 +82,7 @@ object Parser {
# let int = ast.children[0]! as IntegerLiteral
#
# int.value # => '10'
def parse !! ParseError -> Expressions {
def parse !! ParseError -> Body {
let location = @lexer.current_location
let children = Array.new
 
Loading
Loading
@@ -90,7 +90,7 @@ object Parser {
children.push(try top_level_expression(next_token))
}
 
Expressions.new(children: children, location: location)
Body.new(children: children, location: location)
}
 
def top_level_expression(token: Token) !! ParseError -> Node {
Loading
Loading
@@ -380,13 +380,13 @@ object Parser {
 
}
 
def block_body(start: Token) !! ParseError -> Expressions {
def block_body(start: Token) !! ParseError -> Body {
try collect_block_body_nodes(start.location) do (token) {
try expression(token)
}
}
 
def restricted_block_body(start: Token) !! ParseError -> Expressions {
def restricted_block_body(start: Token) !! ParseError -> Body {
try collect_block_body_nodes(start.location) do (token) {
try restricted_block_body_node(token)
}
Loading
Loading
@@ -684,26 +684,26 @@ object Parser {
false: {
let body = try expression(next_token)
 
Expressions.new(children: Array.new(body), location: body.location)
Body.new(children: Array.new(body), location: body.location)
}
)
 
Try.new(
expression: expression,
error_variable: else_var,
else_expression: else_body,
else_body: else_body,
location: token.location
)
}
 
def try_without_else(token: Token, expression: Node) -> Try {
let else_expr =
Expressions.new(children: Array.new, location: token.location)
Body.new(children: Array.new, location: token.location)
 
Try.new(
expression: expression,
error_variable: Nil,
else_expression: else_expr,
else_body: else_expr,
location: token.location
)
}
Loading
Loading
@@ -1217,11 +1217,11 @@ object Parser {
try constant(try token_of_type('constant'))
}
 
def next_as_body !! ParseError -> Expressions {
def next_as_body !! ParseError -> Body {
try block_body(try token_of_type('curly_open'))
}
 
def next_as_restricted_body !! ParseError -> Expressions {
def next_as_restricted_body !! ParseError -> Body {
try restricted_block_body(try token_of_type('curly_open'))
}
 
Loading
Loading
@@ -1262,14 +1262,14 @@ object Parser {
def collect_block_body_nodes(
location: SourceLocation,
block: do (Token) !! ParseError -> Node
) !! ParseError -> Expressions {
) !! ParseError -> Body {
let nodes = Array.new
 
{
let token = next_token
 
(token.type == 'curly_close').if_true {
return Expressions.new(children: nodes, location: location)
return Body.new(children: nodes, location: location)
}
 
nodes.push(try block.call(token))
Loading
Loading
import std::compiler::ast::blocks::*
import std::compiler::ast::body::Body
import std::compiler::ast::comments::*
import std::compiler::ast::expressions::Expressions
import std::compiler::ast::control_flow::*
import std::compiler::ast::imports::Import
import std::compiler::ast::literals::*
import std::compiler::ast::node::Node
import std::compiler::ast::objects::*
import std::compiler::ast::operators::*
import std::compiler::ast::control_flow::*
import std::compiler::ast::send::*
import std::compiler::ast::types::*
import std::compiler::ast::variables::*
Loading
Loading
@@ -25,7 +25,7 @@ def assert_instance_of(node: Node, instance_of: Node) {
assert.true(instance_of(node, instance_of))
}
 
def parse(input: String) -> Expressions {
def parse(input: String) -> Body {
let parser = Parser.new(input: input, file: 'test.inko')
 
try! parser.parse
Loading
Loading
@@ -1528,12 +1528,12 @@ test.group('Parsing try expressions') do (g) {
assert.equal((node.expression as IntegerLiteral).value, '10')
 
assert.equal(node.error_variable, Nil)
assert.true(node.else_expression.children.empty?)
assert.true(node.else_body.children.empty?)
}
 
g.test('Parsing a try with an else expression') {
let node = parse_as(input: 'try 10 else 20', type: Try)
let else_expr = node.else_expression
let else_expr = node.else_body
 
assert.equal((node.expression as IntegerLiteral).value, '10')
assert.equal((else_expr.children[0] as IntegerLiteral).value, '20')
Loading
Loading
@@ -1541,7 +1541,7 @@ test.group('Parsing try expressions') do (g) {
 
g.test('Parsing a try with a multi-line else expression') {
let node = parse_as(input: "try 10 else { 10\n20 }", type: Try)
let else_expr = node.else_expression
let else_expr = node.else_body
 
assert.equal((node.expression as IntegerLiteral).value, '10')
 
Loading
Loading
@@ -1551,7 +1551,7 @@ test.group('Parsing try expressions') do (g) {
 
g.test('Parsing a try with an error argument') {
let node = parse_as(input: 'try 10 else (error) 20', type: Try)
let else_expr = node.else_expression
let else_expr = node.else_body
 
assert.equal((node.expression as IntegerLiteral).value, '10')
assert.equal(node.error_variable!, 'error')
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment