Compare commits
No commits in common. "8c802f2a15eab96e885b42771c7fb218f7c28535" and "9f5e27b28932e5ac813214560a18e9d312eb76f4" have entirely different histories.
8c802f2a15
...
9f5e27b289
@ -1,22 +0,0 @@
|
|||||||
fn main()
|
|
||||||
var x = 42
|
|
||||||
let cl = { i: Int ->
|
|
||||||
let result = x + i
|
|
||||||
x += 1
|
|
||||||
result
|
|
||||||
}
|
|
||||||
println cl(0) // 42
|
|
||||||
println cl(0) // 43
|
|
||||||
println cl(0) // 44
|
|
||||||
end
|
|
||||||
|
|
||||||
fn main()
|
|
||||||
var x = 42
|
|
||||||
fn cl(i: Int): Int
|
|
||||||
let result = x + 1
|
|
||||||
x += 1
|
|
||||||
result
|
|
||||||
end
|
|
||||||
println cl(0) // 42
|
|
||||||
println cl(1) // 42
|
|
||||||
end
|
|
@ -1,32 +0,0 @@
|
|||||||
pub hkt Functor[Self<T>]
|
|
||||||
fn <U> map(f: fn (t: T) -> U) -> Self<U>
|
|
||||||
end
|
|
||||||
|
|
||||||
pub int Iterable<T>
|
|
||||||
fn iter() -> Iterator<T>
|
|
||||||
end
|
|
||||||
|
|
||||||
pub int Iterator<T> = () -> Option<T>
|
|
||||||
|
|
||||||
pub enum Option<T> : Functor[Self<T>]
|
|
||||||
Some(T),
|
|
||||||
None;
|
|
||||||
|
|
||||||
fn unwrap() -> T
|
|
||||||
self is Some(t) ? t : throw Exception('Empty Option')
|
|
||||||
end
|
|
||||||
|
|
||||||
fn expect(msg: String) -> T
|
|
||||||
self is Some(t) ? t : throw Exception(msg)
|
|
||||||
end
|
|
||||||
|
|
||||||
impl Functor
|
|
||||||
fn <U> map(f: fn (t: T) -> U) -> Self<U>
|
|
||||||
self is Some(t) ? Some(f(t)) : None
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
static fn lift(t: T) -> Self<T>
|
|
||||||
Some(t)
|
|
||||||
end
|
|
||||||
end
|
|
@ -1,14 +0,0 @@
|
|||||||
int Greeter
|
|
||||||
fn greet(): Void
|
|
||||||
end
|
|
||||||
|
|
||||||
class MyGreeter(greeting: String) : Greeter
|
|
||||||
impl fn greet()
|
|
||||||
println greeting
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
fn main()
|
|
||||||
let g: Greeter = MyGreeter("Hello, World!")
|
|
||||||
g.greet()
|
|
||||||
end
|
|
@ -1,39 +0,0 @@
|
|||||||
fn main(args: Array<String>)
|
|
||||||
if args[0] == 'greet' then
|
|
||||||
println 'Hello!'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
fn main(args: Array<String>)
|
|
||||||
args.each { println it }
|
|
||||||
end
|
|
||||||
|
|
||||||
fn main(args: Array<String>)
|
|
||||||
if args.find { it == 'greet' } then
|
|
||||||
println 'There was a greet arg'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
// longer
|
|
||||||
fn main(args: Array<String>)
|
|
||||||
let findIndexResult: Option<Int> = args.findIndex { it == 'greet' }
|
|
||||||
if findIndexResult.isSome() then
|
|
||||||
let index: Int = findIndex.unwrap()
|
|
||||||
println "greet arg was index ${index}"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
// shorter with destructuring
|
|
||||||
fn main(args: Array<String>)
|
|
||||||
if let Some(greetIndex) = args.findIndex { it == 'greet' } then
|
|
||||||
println "greet arg was index ${greetIndex}"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
fn main(args: Array<String>)
|
|
||||||
let parsed: CliArgs = std::cli::parse(args)
|
|
||||||
let greetingCount = parsed.get::<Int>('--count', '-c').orElse(1)
|
|
||||||
if let Some(greeting) = parsed.get('greeting', 'g') then
|
|
||||||
greetingCount.times { println greeting }
|
|
||||||
end
|
|
||||||
end
|
|
@ -1,105 +0,0 @@
|
|||||||
pub int Display
|
|
||||||
fn toString() -> String
|
|
||||||
end
|
|
||||||
|
|
||||||
pub int LazyDisplay
|
|
||||||
fn toLazyString() -> DString
|
|
||||||
end
|
|
||||||
|
|
||||||
pub int DString : Display
|
|
||||||
parts: Array<DStringPart>
|
|
||||||
end
|
|
||||||
|
|
||||||
pub enum DStringPart
|
|
||||||
Constant(String),
|
|
||||||
Display(Display),
|
|
||||||
Lazy(Fn<Display>)
|
|
||||||
end
|
|
||||||
|
|
||||||
#internal
|
|
||||||
class DStringImpl(parts) : DString
|
|
||||||
cachedEval: Option<String>
|
|
||||||
canCache: Option<Boolean>
|
|
||||||
|
|
||||||
fn getCanCache() -> Boolean
|
|
||||||
for part in parts do
|
|
||||||
if part is Lazy then return false end
|
|
||||||
end
|
|
||||||
true
|
|
||||||
end
|
|
||||||
|
|
||||||
impl Display
|
|
||||||
pub fn toString() -> String
|
|
||||||
if cachedEval is Some(s) then
|
|
||||||
return s
|
|
||||||
end
|
|
||||||
if canCache is None then
|
|
||||||
canCache = Some(getCanCache())
|
|
||||||
end
|
|
||||||
// Note the groovy-like property getter
|
|
||||||
if canCache.truthy then
|
|
||||||
cachedEval = Some(
|
|
||||||
parts.reduce('') { acc, part ->
|
|
||||||
let partAsString = match part as
|
|
||||||
Constant(s) => s,
|
|
||||||
Display(d) => d.toString(),
|
|
||||||
Lazy(f) => throw DumbProgrammerException(
|
|
||||||
'Cannot cache when parts contains a Lazy.'
|
|
||||||
)
|
|
||||||
end
|
|
||||||
acc + partAsString
|
|
||||||
}
|
|
||||||
)
|
|
||||||
cachedEval.unwrap()
|
|
||||||
else
|
|
||||||
parts.reduce('') { acc, part ->
|
|
||||||
let partAsString = match part as
|
|
||||||
Constant(s) => s,
|
|
||||||
Display(d) => d.toString(),
|
|
||||||
Lazy(f) => f().toString()
|
|
||||||
end
|
|
||||||
acc + partAsString
|
|
||||||
}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
// Special "extension" implementation of Option for all T where T : Boolean
|
|
||||||
pub int HasTruth
|
|
||||||
fn isTruthy() -> Boolean
|
|
||||||
end
|
|
||||||
|
|
||||||
impl HasTruth for Option<T> where T is Boolean
|
|
||||||
pub fn isTruthy() -> Boolean
|
|
||||||
self is Some(b) ? b : false
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
// some other file
|
|
||||||
use std::extensions::HasTruth[Option<Boolean>]
|
|
||||||
|
|
||||||
fn maybeSomeOrNone -> Option<Boolean>
|
|
||||||
Some(true)
|
|
||||||
end
|
|
||||||
|
|
||||||
fn main()
|
|
||||||
let o = maybeSomeOrNone()
|
|
||||||
if o.truthy then
|
|
||||||
println "It's truthy!"
|
|
||||||
else
|
|
||||||
println "It's falsy..."
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
fn stringDemo() -> Void
|
|
||||||
let plain = 'Hello, World!'
|
|
||||||
let x = 42
|
|
||||||
let withDisplay = "Hello! x is $x"
|
|
||||||
let withLazy = "Hello! x is ${ x + 42 }"
|
|
||||||
println plain // Hello, World!
|
|
||||||
println withDisplay // Hello! x is 42
|
|
||||||
println withLazy // Hello! x is 84
|
|
||||||
x += 42
|
|
||||||
println withLazy // Hello! x is 126
|
|
||||||
end
|
|
@ -1,41 +0,0 @@
|
|||||||
pub trait HasTruth
|
|
||||||
fn isTruthy() -> Boolean
|
|
||||||
|
|
||||||
def fn isFalsy() -> Boolean = !self.truthy
|
|
||||||
end
|
|
||||||
|
|
||||||
pub trait Functor[Self<T>]
|
|
||||||
fn <U> map(f: fn (t: T) -> U) -> Self<U>
|
|
||||||
end
|
|
||||||
|
|
||||||
pub trait Lift[Self<T>]
|
|
||||||
static fn lift(t: T) -> Self<T>
|
|
||||||
end
|
|
||||||
|
|
||||||
pub trait Applicative[Self<T>, U, V] : Functor[Self] + Lift[Self] where T is fn (u: U) -> V
|
|
||||||
fn apply(us: Self<U>) -> Self<V>
|
|
||||||
end
|
|
||||||
|
|
||||||
pub trait Monad[Self<T>] : Functor[Self] + Lift[Self]
|
|
||||||
fn <U> flatMap(f: fn (t: T) -> Self<U>) -> Self<U>
|
|
||||||
end
|
|
||||||
|
|
||||||
// Traits as higher-kinded types
|
|
||||||
// - Cannot be used as concrete types, casted, etc.
|
|
||||||
// - Cannot be constructed/instantiated
|
|
||||||
// - Imports are different:
|
|
||||||
// ```
|
|
||||||
// use std::core::Option
|
|
||||||
// use std::trait::Monad[Option<*>] // bring in Monad for all Option<*>
|
|
||||||
// ```
|
|
||||||
// - Once imported, fns are in scope
|
|
||||||
// - Can also be looked-up 'dictionary' style:
|
|
||||||
// ```
|
|
||||||
// use std::trait::HasTruth[Option<Boolean>]
|
|
||||||
//
|
|
||||||
// fn main()
|
|
||||||
// let maybeTruthy: Option<Boolean> = someOtherApiCall()
|
|
||||||
// if HasTruth[maybeTruthy].isTruthy() then
|
|
||||||
// println "It's truthy."
|
|
||||||
// end
|
|
||||||
// ```
|
|
@ -427,37 +427,29 @@ fn build_namespace(file_id: usize, namespace_pair: Pair<Rule>) -> FullyQualified
|
|||||||
|
|
||||||
fn build_use_statement(file_id: usize, use_statement_pair: Pair<Rule>) -> UseStatement {
|
fn build_use_statement(file_id: usize, use_statement_pair: Pair<Rule>) -> UseStatement {
|
||||||
let as_span = use_statement_pair.as_span();
|
let as_span = use_statement_pair.as_span();
|
||||||
let range = Range {
|
|
||||||
start: as_span.start(),
|
|
||||||
end: as_span.end(),
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut inner = use_statement_pair.into_inner();
|
let mut inner = use_statement_pair.into_inner();
|
||||||
let inner_length = inner.len();
|
let inner_length = inner.len();
|
||||||
|
|
||||||
inner.next().unwrap(); // use
|
inner.next().unwrap(); // use
|
||||||
|
|
||||||
let mut prefixes = vec![];
|
let mut identifiers = vec![];
|
||||||
let mut built: Option<UseStatement> = None;
|
let mut last = None;
|
||||||
|
|
||||||
for (i, inner_pair) in inner.into_iter().enumerate() {
|
for (i, inner_pair) in inner.into_iter().enumerate() {
|
||||||
if i != inner_length - 2 {
|
if i != inner_length - 2 {
|
||||||
prefixes.push(inner_pair.as_str());
|
identifiers.push(Box::new(expect_and_use(
|
||||||
} else {
|
|
||||||
built = Some(match inner_pair.as_rule() {
|
|
||||||
Rule::Identifier => UseStatement::Concrete(ConcreteUseStatement::new(
|
|
||||||
prefixes.iter().map(|s| s.to_string()).collect(),
|
|
||||||
vec![Box::new(expect_and_use(
|
|
||||||
file_id,
|
file_id,
|
||||||
inner_pair,
|
inner_pair,
|
||||||
Rule::Identifier,
|
Rule::Identifier,
|
||||||
build_identifier,
|
build_identifier,
|
||||||
))],
|
)));
|
||||||
file_id,
|
} else {
|
||||||
range,
|
last = Some(Box::new(match inner_pair.as_rule() {
|
||||||
)),
|
Rule::Identifier => {
|
||||||
Rule::UseList => UseStatement::Concrete(ConcreteUseStatement::new(
|
UseStatementLast::Identifier(Box::new(build_identifier(file_id, inner_pair)))
|
||||||
prefixes.iter().map(|s| s.to_string()).collect(),
|
}
|
||||||
|
Rule::Star => UseStatementLast::Star,
|
||||||
|
Rule::UseList => UseStatementLast::Identifiers(
|
||||||
inner_pair
|
inner_pair
|
||||||
.into_inner()
|
.into_inner()
|
||||||
.map(|identifier_pair| {
|
.map(|identifier_pair| {
|
||||||
@ -469,20 +461,21 @@ fn build_use_statement(file_id: usize, use_statement_pair: Pair<Rule>) -> UseSta
|
|||||||
))
|
))
|
||||||
})
|
})
|
||||||
.collect(),
|
.collect(),
|
||||||
file_id,
|
),
|
||||||
range,
|
|
||||||
)),
|
|
||||||
Rule::Star => UseStatement::Star(StarUseStatement::new(
|
|
||||||
prefixes.iter().map(|s| s.to_string()).collect(),
|
|
||||||
file_id,
|
|
||||||
range,
|
|
||||||
)),
|
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
});
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
built.unwrap()
|
UseStatement::new(
|
||||||
|
identifiers,
|
||||||
|
last.unwrap(),
|
||||||
|
file_id,
|
||||||
|
Range {
|
||||||
|
start: as_span.start(),
|
||||||
|
end: as_span.end(),
|
||||||
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_module_level_declaration(file_id: usize, pair: Pair<Rule>) -> ModuleLevelDeclaration {
|
fn build_module_level_declaration(file_id: usize, pair: Pair<Rule>) -> ModuleLevelDeclaration {
|
||||||
|
@ -319,14 +319,21 @@ impl NodeInner for CompilationUnit {
|
|||||||
impl NodeInner for UseStatement {
|
impl NodeInner for UseStatement {
|
||||||
fn children(&self) -> Vec<NodeRef> {
|
fn children(&self) -> Vec<NodeRef> {
|
||||||
let mut children = vec![];
|
let mut children = vec![];
|
||||||
match self {
|
children.extend(
|
||||||
UseStatement::Concrete(concrete_use_statement) => children.extend(
|
self.identifiers()
|
||||||
concrete_use_statement
|
|
||||||
.identifiers()
|
|
||||||
.iter()
|
.iter()
|
||||||
.map(|identifier| NodeRef::Identifier(*identifier))
|
.map(|identifier| NodeRef::Identifier(*identifier)),
|
||||||
|
);
|
||||||
|
match self.last() {
|
||||||
|
UseStatementLast::Identifier(identifier) => {
|
||||||
|
children.push(NodeRef::Identifier(identifier))
|
||||||
|
}
|
||||||
|
UseStatementLast::Identifiers(identifiers) => children.extend(
|
||||||
|
identifiers
|
||||||
|
.iter()
|
||||||
|
.map(|identifier| NodeRef::Identifier(identifier.clone())),
|
||||||
),
|
),
|
||||||
UseStatement::Star(_) => {}
|
UseStatementLast::Star => {}
|
||||||
}
|
}
|
||||||
children
|
children
|
||||||
}
|
}
|
||||||
|
@ -1,48 +1,75 @@
|
|||||||
use crate::ast::node::named::Named;
|
use crate::ast::node::named::Named;
|
||||||
use crate::ast::node::names::Identifier;
|
use crate::ast::node::names::Identifier;
|
||||||
|
use std::borrow::Cow;
|
||||||
use std::range::Range;
|
use std::range::Range;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum UseStatement {
|
pub struct UseStatement {
|
||||||
Concrete(ConcreteUseStatement),
|
|
||||||
Star(StarUseStatement),
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct ConcreteUseStatement {
|
|
||||||
prefixes: Vec<String>,
|
|
||||||
identifiers: Vec<Box<Identifier>>,
|
identifiers: Vec<Box<Identifier>>,
|
||||||
|
last: Box<UseStatementLast>,
|
||||||
file_id: usize,
|
file_id: usize,
|
||||||
range: Range<usize>,
|
range: Range<usize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ConcreteUseStatement {
|
impl UseStatement {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
prefixes: Vec<String>,
|
|
||||||
identifiers: Vec<Box<Identifier>>,
|
identifiers: Vec<Box<Identifier>>,
|
||||||
|
last: Box<UseStatementLast>,
|
||||||
file_id: usize,
|
file_id: usize,
|
||||||
range: Range<usize>,
|
range: Range<usize>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
ConcreteUseStatement {
|
UseStatement {
|
||||||
prefixes,
|
|
||||||
identifiers,
|
identifiers,
|
||||||
|
last,
|
||||||
file_id,
|
file_id,
|
||||||
range,
|
range,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn prefixes(&self) -> &Vec<String> {
|
pub fn base_name(&self) -> Cow<'_, str> {
|
||||||
&self.prefixes
|
use UseStatementLast::*;
|
||||||
|
if self.identifiers.is_empty() {
|
||||||
|
match self.last.as_ref() {
|
||||||
|
Identifier(_) => Cow::from(""),
|
||||||
|
Star | Identifiers(_) => panic!(), // should never get here because of grammar
|
||||||
|
}
|
||||||
|
} else if self.identifiers.len() == 1 {
|
||||||
|
self.identifiers[0].name()
|
||||||
|
} else {
|
||||||
|
let mut acc = String::new();
|
||||||
|
for (i, identifier) in self.identifiers.iter().enumerate() {
|
||||||
|
acc.push_str(&identifier.name());
|
||||||
|
if i != self.identifiers.len() - 1 {
|
||||||
|
acc.push_str("::");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Cow::from(acc)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn join_prefixes(&self) -> String {
|
pub fn is_star(&self) -> bool {
|
||||||
self.prefixes.join("::")
|
match self.last.as_ref() {
|
||||||
|
UseStatementLast::Star => true,
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn identifiers(&self) -> Vec<&Identifier> {
|
pub fn identifiers(&self) -> Vec<&Identifier> {
|
||||||
self.identifiers.iter().map(Box::as_ref).collect()
|
self.identifiers.iter().map(Box::as_ref).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn identifiers_mut(&mut self) -> Vec<&mut Identifier> {
|
||||||
|
self.identifiers.iter_mut().map(Box::as_mut).collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn last(&self) -> &UseStatementLast {
|
||||||
|
&self.last
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn last_mut(&mut self) -> &mut UseStatementLast {
|
||||||
|
&mut self.last
|
||||||
|
}
|
||||||
|
|
||||||
pub fn file_id(&self) -> usize {
|
pub fn file_id(&self) -> usize {
|
||||||
self.file_id
|
self.file_id
|
||||||
}
|
}
|
||||||
@ -53,34 +80,8 @@ impl ConcreteUseStatement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct StarUseStatement {
|
pub enum UseStatementLast {
|
||||||
prefixes: Vec<String>,
|
Identifier(Box<Identifier>),
|
||||||
file_id: usize,
|
Identifiers(Vec<Box<Identifier>>),
|
||||||
range: Range<usize>,
|
Star,
|
||||||
}
|
|
||||||
|
|
||||||
impl StarUseStatement {
|
|
||||||
pub fn new(prefixes: Vec<String>, file_id: usize, range: Range<usize>) -> Self {
|
|
||||||
Self {
|
|
||||||
prefixes,
|
|
||||||
file_id,
|
|
||||||
range,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn prefixes(&self) -> &Vec<String> {
|
|
||||||
&self.prefixes
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn join_prefixes(&self) -> String {
|
|
||||||
self.prefixes.join("::")
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn file_id(&self) -> usize {
|
|
||||||
self.file_id
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn range(&self) -> Range<usize> {
|
|
||||||
self.range
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -320,28 +320,6 @@ impl PrettyPrint for CompilationUnit {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PrettyPrint for UseStatement {
|
|
||||||
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
|
|
||||||
writer.writeln_indented("UseStatement")?;
|
|
||||||
writer.increase_indent();
|
|
||||||
match self {
|
|
||||||
UseStatement::Concrete(concrete_use_statement) => {
|
|
||||||
writer.writeln_indented(&format!("ConcreteUseStatement(prefixes = {})", concrete_use_statement.join_prefixes()))?;
|
|
||||||
writer.increase_indent();
|
|
||||||
for identifier in concrete_use_statement.identifiers() {
|
|
||||||
identifier.pretty_print(writer)?;
|
|
||||||
}
|
|
||||||
writer.decrease_indent();
|
|
||||||
}
|
|
||||||
UseStatement::Star(star_use_statement) => {
|
|
||||||
writer.writeln_indented(&format!("StarUseStatement(prefixes = {})", star_use_statement.join_prefixes()))?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
writer.decrease_indent();
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PrettyPrint for ModuleLevelDeclaration {
|
impl PrettyPrint for ModuleLevelDeclaration {
|
||||||
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
|
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
|
||||||
use crate::ast::node::level::ModuleLevelDeclaration::*;
|
use crate::ast::node::level::ModuleLevelDeclaration::*;
|
||||||
@ -640,6 +618,39 @@ impl PrettyPrint for FieldDeclaration {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl PrettyPrint for UseStatement {
|
||||||
|
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
|
||||||
|
writer.writeln_indented("UseStatement")?;
|
||||||
|
writer.increase_indent();
|
||||||
|
for identifier in self.identifiers() {
|
||||||
|
identifier.pretty_print(writer)?;
|
||||||
|
}
|
||||||
|
self.last().pretty_print(writer)?;
|
||||||
|
writer.decrease_indent();
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PrettyPrint for UseStatementLast {
|
||||||
|
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
|
||||||
|
writer.writeln_indented("UseStatementLast")?;
|
||||||
|
writer.increase_indent();
|
||||||
|
match self {
|
||||||
|
UseStatementLast::Identifier(i) => i.pretty_print(writer)?,
|
||||||
|
UseStatementLast::Identifiers(is) => {
|
||||||
|
for i in is {
|
||||||
|
i.pretty_print(writer)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
UseStatementLast::Star => {
|
||||||
|
writer.writeln_indented("Star")?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
writer.decrease_indent();
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl PrettyPrint for BlockStatement {
|
impl PrettyPrint for BlockStatement {
|
||||||
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
|
fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
|
||||||
writer.writeln_indented("BlockStatement")?;
|
writer.writeln_indented("BlockStatement")?;
|
||||||
|
@ -410,13 +410,23 @@ impl Unparse for CompilationUnit {
|
|||||||
impl Unparse for UseStatement {
|
impl Unparse for UseStatement {
|
||||||
fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
|
fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
|
||||||
writer.write_indented("use ")?;
|
writer.write_indented("use ")?;
|
||||||
|
for (i, identifier) in self.identifiers().iter().enumerate() {
|
||||||
|
identifier.unparse(writer)?;
|
||||||
|
if i != self.identifiers().len() - 2 {
|
||||||
|
// 2 because of use
|
||||||
|
writer.write("::")?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.last().unparse(writer)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Unparse for UseStatementLast {
|
||||||
|
fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> {
|
||||||
match self {
|
match self {
|
||||||
UseStatement::Concrete(concrete_use_statement) => {
|
UseStatementLast::Star => writer.write("*"),
|
||||||
writer.write(&format!("{}::", concrete_use_statement.join_prefixes()))?;
|
UseStatementLast::Identifiers(identifiers) => {
|
||||||
let identifiers = concrete_use_statement.identifiers();
|
|
||||||
if identifiers.len() == 1 {
|
|
||||||
identifiers[0].unparse(writer)?;
|
|
||||||
} else {
|
|
||||||
writer.write("{")?;
|
writer.write("{")?;
|
||||||
for (i, identifier) in identifiers.iter().enumerate() {
|
for (i, identifier) in identifiers.iter().enumerate() {
|
||||||
identifier.unparse(writer)?;
|
identifier.unparse(writer)?;
|
||||||
@ -425,14 +435,11 @@ impl Unparse for UseStatement {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
writer.write("}")?;
|
writer.write("}")?;
|
||||||
}
|
|
||||||
},
|
|
||||||
UseStatement::Star(star_use_statement) => {
|
|
||||||
writer.write(&format!("{}::*", star_use_statement.join_prefixes()))?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
UseStatementLast::Identifier(i) => i.unparse(writer),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Declarations allowed in each level */
|
/* Declarations allowed in each level */
|
||||||
|
@ -364,8 +364,22 @@ fn gather_use_statement(
|
|||||||
fqn_context: &mut FqnContext,
|
fqn_context: &mut FqnContext,
|
||||||
diagnostics: &mut Vec<DmDiagnostic>,
|
diagnostics: &mut Vec<DmDiagnostic>,
|
||||||
) {
|
) {
|
||||||
|
if use_statement.is_star() {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
let base_name = use_statement.base_name().to_string();
|
||||||
|
match use_statement.last_mut() {
|
||||||
|
UseStatementLast::Identifier(identifier) => {
|
||||||
|
handle_use_statement_import(symbol_table, &base_name, identifier, diagnostics)
|
||||||
|
}
|
||||||
|
UseStatementLast::Identifiers(identifiers) => {
|
||||||
|
for identifier in identifiers {
|
||||||
|
handle_use_statement_import(symbol_table, &base_name, identifier, diagnostics)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
UseStatementLast::Star => panic!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* Declarations allowed in each level */
|
/* Declarations allowed in each level */
|
||||||
|
|
||||||
|
@ -56,6 +56,7 @@ mod tests {
|
|||||||
use super::*;
|
use super::*;
|
||||||
use crate::ast::build::build_ast;
|
use crate::ast::build::build_ast;
|
||||||
use crate::ast::children::NodeRef;
|
use crate::ast::children::NodeRef;
|
||||||
|
use crate::ast::node::use_statement::UseStatementLast;
|
||||||
use crate::ast::walk::walk_depth_first;
|
use crate::ast::walk::walk_depth_first;
|
||||||
use crate::parser::{DeimosParser, Rule};
|
use crate::parser::{DeimosParser, Rule};
|
||||||
use crate::std_core::add_std_core_symbols;
|
use crate::std_core::add_std_core_symbols;
|
||||||
@ -125,7 +126,25 @@ mod tests {
|
|||||||
panic!("{:?} does not have a saved symbol.", fqn)
|
panic!("{:?} does not have a saved symbol.", fqn)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
NodeRef::UseStatement(use_statement) => match use_statement {
|
NodeRef::UseStatement(use_statement) => match use_statement.last() {
|
||||||
|
UseStatementLast::Identifier(identifier) => {
|
||||||
|
if identifier.saved_symbol().is_none() {
|
||||||
|
panic!(
|
||||||
|
"UseStatement {:?} does not have a saved symbol.",
|
||||||
|
identifier
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
UseStatementLast::Identifiers(identifiers) => {
|
||||||
|
for identifier in identifiers {
|
||||||
|
if identifier.saved_symbol().is_none() {
|
||||||
|
panic!(
|
||||||
|
"UseStatement {:?} does not have a saved symbol.",
|
||||||
|
identifier
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
_ => todo!(),
|
_ => todo!(),
|
||||||
},
|
},
|
||||||
_ => {}
|
_ => {}
|
||||||
@ -134,7 +153,35 @@ mod tests {
|
|||||||
|
|
||||||
fn assert_resolved_symbols(compilation_unit: &CompilationUnit) {
|
fn assert_resolved_symbols(compilation_unit: &CompilationUnit) {
|
||||||
walk_depth_first(compilation_unit, &mut |node_ref| match node_ref {
|
walk_depth_first(compilation_unit, &mut |node_ref| match node_ref {
|
||||||
NodeRef::UseStatement(use_statement) => match use_statement {
|
NodeRef::UseStatement(use_statement) => match use_statement.last() {
|
||||||
|
UseStatementLast::Identifier(identifier) => {
|
||||||
|
let use_statement_symbol = identifier
|
||||||
|
.saved_symbol()
|
||||||
|
.unwrap()
|
||||||
|
.unwrap_use_statement_symbol();
|
||||||
|
let borrowed = use_statement_symbol.borrow();
|
||||||
|
if borrowed.referenced_symbol().is_none() {
|
||||||
|
panic!(
|
||||||
|
"{:?} does not have a referenced symbol.",
|
||||||
|
use_statement_symbol
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
UseStatementLast::Identifiers(identifiers) => {
|
||||||
|
for identifier in identifiers {
|
||||||
|
let use_statement_symbol = identifier
|
||||||
|
.saved_symbol()
|
||||||
|
.unwrap()
|
||||||
|
.unwrap_use_statement_symbol();
|
||||||
|
let borrowed = use_statement_symbol.borrow();
|
||||||
|
if borrowed.referenced_symbol().is_none() {
|
||||||
|
panic!(
|
||||||
|
"{:?} does not have a referenced symbol.",
|
||||||
|
use_statement_symbol
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
_ => todo!(),
|
_ => todo!(),
|
||||||
},
|
},
|
||||||
_ => {}
|
_ => {}
|
||||||
|
@ -260,7 +260,36 @@ fn resolve_use_statement(
|
|||||||
symbol_table: &mut SymbolTable,
|
symbol_table: &mut SymbolTable,
|
||||||
diagnostics: &mut Vec<DmDiagnostic>,
|
diagnostics: &mut Vec<DmDiagnostic>,
|
||||||
) {
|
) {
|
||||||
todo!()
|
let file_id = use_statement.file_id();
|
||||||
|
let use_statement_range = use_statement.range();
|
||||||
|
let base_name = use_statement.base_name().to_string();
|
||||||
|
|
||||||
|
match use_statement.last_mut() {
|
||||||
|
UseStatementLast::Identifier(identifier) => {
|
||||||
|
handle_use_statement_identifier(
|
||||||
|
identifier.deref_mut(),
|
||||||
|
&base_name,
|
||||||
|
symbol_table,
|
||||||
|
diagnostics,
|
||||||
|
file_id,
|
||||||
|
use_statement_range,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
UseStatementLast::Identifiers(identifiers) => {
|
||||||
|
for identifier in identifiers {
|
||||||
|
let identifier_range = identifier.range();
|
||||||
|
handle_use_statement_identifier(
|
||||||
|
identifier.deref_mut(),
|
||||||
|
&base_name,
|
||||||
|
symbol_table,
|
||||||
|
diagnostics,
|
||||||
|
file_id,
|
||||||
|
identifier_range,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
UseStatementLast::Star => todo!("star imports"),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Declarations allowed in each level */
|
/* Declarations allowed in each level */
|
||||||
|
@ -29,6 +29,14 @@ impl SourceDefinition {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[deprecated(note = "Use identifier instead.")]
|
||||||
|
pub fn from_use_statement(use_statement: &UseStatement) -> Self {
|
||||||
|
SourceDefinition {
|
||||||
|
file_id: use_statement.file_id(),
|
||||||
|
range: use_statement.range(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn file_id(&self) -> usize {
|
pub fn file_id(&self) -> usize {
|
||||||
self.file_id
|
self.file_id
|
||||||
}
|
}
|
||||||
|
@ -333,20 +333,6 @@ Namespace = {
|
|||||||
~ FullyQualifiedName
|
~ FullyQualifiedName
|
||||||
}
|
}
|
||||||
|
|
||||||
UseStatement = {
|
|
||||||
Use
|
|
||||||
~ Identifier
|
|
||||||
~ ( "::" ~ Identifier )*
|
|
||||||
~ ( "::" ~ ( Star | UseList ) )?
|
|
||||||
}
|
|
||||||
|
|
||||||
UseList = {
|
|
||||||
"{"
|
|
||||||
~ Identifier
|
|
||||||
~ ( "," ~ Identifier )*
|
|
||||||
~ "}"
|
|
||||||
}
|
|
||||||
|
|
||||||
// Organizational declarations
|
// Organizational declarations
|
||||||
|
|
||||||
ModuleLevelDeclaration = {
|
ModuleLevelDeclaration = {
|
||||||
@ -537,6 +523,20 @@ Field = {
|
|||||||
|
|
||||||
// Statements
|
// Statements
|
||||||
|
|
||||||
|
UseStatement = {
|
||||||
|
Use
|
||||||
|
~ Identifier
|
||||||
|
~ ( "::" ~ Identifier )*
|
||||||
|
~ ( "::" ~ ( Star | UseList ) )?
|
||||||
|
}
|
||||||
|
|
||||||
|
UseList = {
|
||||||
|
"{"
|
||||||
|
~ Identifier
|
||||||
|
~ ( "," ~ Identifier )*
|
||||||
|
~ "}"
|
||||||
|
}
|
||||||
|
|
||||||
BlockStatement = {
|
BlockStatement = {
|
||||||
"{"
|
"{"
|
||||||
~ Statement*
|
~ Statement*
|
||||||
|
Loading…
Reference in New Issue
Block a user