2022-12-13 13:34:52 -05:00
|
|
|
// Lower NIR into AIR
|
|
|
|
//
|
2023-01-17 23:09:25 -05:00
|
|
|
// Copyright (C) 2014-2023 Ryan Specialty, LLC.
|
2022-12-13 13:34:52 -05:00
|
|
|
//
|
|
|
|
// This file is part of TAME.
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
//! Lower [NIR](super) into [AIR](crate::asg::air).
|
|
|
|
|
|
|
|
use std::{error::Error, fmt::Display};
|
|
|
|
|
2023-01-23 16:30:25 -05:00
|
|
|
use crate::{
|
tamer: Very basic support for template application NIR -> xmli
This this a big change that's difficult to break up, and I don't have the
energy after it.
This introduces nullary template application, short- and long-form. Note
that a body of the short form is a `@values@` argument, so that's not
supported yet.
This continues to formalize the idea of what "template application" and
"template expansion" mean in TAMER. It makes a separate `TplApply`
unnecessary, because now application is simply a reference to a
template. Expansion and application are one and the same: when a template
expands, it'll re-bind metavariables to the parent context. So in a
template context, this amounts to application.
But applying a closed template will have nothing to bind, and so is
equivalent to expansion. And since `Meta` objects are not valid outside of
a `Tpl` context, applying a non-closed template outside of another template
will be invalid.
So we get all of this with a single primitive (getting the "value" of a
template).
The expansion is conceptually like `,@` in Lisp, where we're splicing trees.
It's a mess in some spots, but I want to get this committed before I do a
little bit of cleanup.
2023-03-17 10:25:56 -04:00
|
|
|
asg::air::Air, diagnose::Diagnostic, parse::prelude::*, span::UNKNOWN_SPAN,
|
|
|
|
};
|
|
|
|
|
|
|
|
// These are also used by the `test` module which imports `super`.
|
|
|
|
#[cfg(feature = "wip-nir-to-air")]
|
2023-03-21 14:38:07 -04:00
|
|
|
use crate::{asg::ExprOp, nir::NirEntity};
|
2022-12-13 13:34:52 -05:00
|
|
|
|
|
|
|
use super::Nir;
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Eq, Default)]
|
|
|
|
pub enum NirToAir {
|
|
|
|
#[default]
|
|
|
|
Ready,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for NirToAir {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
|
|
use NirToAir::*;
|
|
|
|
|
|
|
|
match self {
|
|
|
|
Ready => write!(f, "ready to lower NIR to AIR"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
tamer: Very basic support for template application NIR -> xmli
This this a big change that's difficult to break up, and I don't have the
energy after it.
This introduces nullary template application, short- and long-form. Note
that a body of the short form is a `@values@` argument, so that's not
supported yet.
This continues to formalize the idea of what "template application" and
"template expansion" mean in TAMER. It makes a separate `TplApply`
unnecessary, because now application is simply a reference to a
template. Expansion and application are one and the same: when a template
expands, it'll re-bind metavariables to the parent context. So in a
template context, this amounts to application.
But applying a closed template will have nothing to bind, and so is
equivalent to expansion. And since `Meta` objects are not valid outside of
a `Tpl` context, applying a non-closed template outside of another template
will be invalid.
So we get all of this with a single primitive (getting the "value" of a
template).
The expansion is conceptually like `,@` in Lisp, where we're splicing trees.
It's a mess in some spots, but I want to get this committed before I do a
little bit of cleanup.
2023-03-17 10:25:56 -04:00
|
|
|
type QueuedObj = Option<Air>;
|
|
|
|
|
2022-12-13 13:34:52 -05:00
|
|
|
impl ParseState for NirToAir {
|
|
|
|
type Token = Nir;
|
|
|
|
type Object = Air;
|
|
|
|
type Error = NirToAirError;
|
tamer: Very basic support for template application NIR -> xmli
This this a big change that's difficult to break up, and I don't have the
energy after it.
This introduces nullary template application, short- and long-form. Note
that a body of the short form is a `@values@` argument, so that's not
supported yet.
This continues to formalize the idea of what "template application" and
"template expansion" mean in TAMER. It makes a separate `TplApply`
unnecessary, because now application is simply a reference to a
template. Expansion and application are one and the same: when a template
expands, it'll re-bind metavariables to the parent context. So in a
template context, this amounts to application.
But applying a closed template will have nothing to bind, and so is
equivalent to expansion. And since `Meta` objects are not valid outside of
a `Tpl` context, applying a non-closed template outside of another template
will be invalid.
So we get all of this with a single primitive (getting the "value" of a
template).
The expansion is conceptually like `,@` in Lisp, where we're splicing trees.
It's a mess in some spots, but I want to get this committed before I do a
little bit of cleanup.
2023-03-17 10:25:56 -04:00
|
|
|
type Context = QueuedObj;
|
2022-12-13 13:34:52 -05:00
|
|
|
|
tamer: Very basic support for template application NIR -> xmli
This this a big change that's difficult to break up, and I don't have the
energy after it.
This introduces nullary template application, short- and long-form. Note
that a body of the short form is a `@values@` argument, so that's not
supported yet.
This continues to formalize the idea of what "template application" and
"template expansion" mean in TAMER. It makes a separate `TplApply`
unnecessary, because now application is simply a reference to a
template. Expansion and application are one and the same: when a template
expands, it'll re-bind metavariables to the parent context. So in a
template context, this amounts to application.
But applying a closed template will have nothing to bind, and so is
equivalent to expansion. And since `Meta` objects are not valid outside of
a `Tpl` context, applying a non-closed template outside of another template
will be invalid.
So we get all of this with a single primitive (getting the "value" of a
template).
The expansion is conceptually like `,@` in Lisp, where we're splicing trees.
It's a mess in some spots, but I want to get this committed before I do a
little bit of cleanup.
2023-03-17 10:25:56 -04:00
|
|
|
#[cfg(not(feature = "wip-nir-to-air"))]
|
2022-12-13 13:34:52 -05:00
|
|
|
fn parse_token(
|
|
|
|
self,
|
2023-01-23 16:30:25 -05:00
|
|
|
tok: Self::Token,
|
tamer: Very basic support for template application NIR -> xmli
This this a big change that's difficult to break up, and I don't have the
energy after it.
This introduces nullary template application, short- and long-form. Note
that a body of the short form is a `@values@` argument, so that's not
supported yet.
This continues to formalize the idea of what "template application" and
"template expansion" mean in TAMER. It makes a separate `TplApply`
unnecessary, because now application is simply a reference to a
template. Expansion and application are one and the same: when a template
expands, it'll re-bind metavariables to the parent context. So in a
template context, this amounts to application.
But applying a closed template will have nothing to bind, and so is
equivalent to expansion. And since `Meta` objects are not valid outside of
a `Tpl` context, applying a non-closed template outside of another template
will be invalid.
So we get all of this with a single primitive (getting the "value" of a
template).
The expansion is conceptually like `,@` in Lisp, where we're splicing trees.
It's a mess in some spots, but I want to get this committed before I do a
little bit of cleanup.
2023-03-17 10:25:56 -04:00
|
|
|
_queue: &mut Self::Context,
|
2022-12-13 13:34:52 -05:00
|
|
|
) -> TransitionResult<Self::Super> {
|
2023-01-23 16:30:25 -05:00
|
|
|
use NirToAir::*;
|
|
|
|
|
tamer: Very basic support for template application NIR -> xmli
This this a big change that's difficult to break up, and I don't have the
energy after it.
This introduces nullary template application, short- and long-form. Note
that a body of the short form is a `@values@` argument, so that's not
supported yet.
This continues to formalize the idea of what "template application" and
"template expansion" mean in TAMER. It makes a separate `TplApply`
unnecessary, because now application is simply a reference to a
template. Expansion and application are one and the same: when a template
expands, it'll re-bind metavariables to the parent context. So in a
template context, this amounts to application.
But applying a closed template will have nothing to bind, and so is
equivalent to expansion. And since `Meta` objects are not valid outside of
a `Tpl` context, applying a non-closed template outside of another template
will be invalid.
So we get all of this with a single primitive (getting the "value" of a
template).
The expansion is conceptually like `,@` in Lisp, where we're splicing trees.
It's a mess in some spots, but I want to get this committed before I do a
little bit of cleanup.
2023-03-17 10:25:56 -04:00
|
|
|
let _ = tok; // prevent `unused_variables` warning
|
|
|
|
Transition(Ready).ok(Air::Todo(UNKNOWN_SPAN))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "wip-nir-to-air")]
|
|
|
|
fn parse_token(
|
|
|
|
self,
|
|
|
|
tok: Self::Token,
|
|
|
|
queue: &mut Self::Context,
|
|
|
|
) -> TransitionResult<Self::Super> {
|
|
|
|
use NirToAir::*;
|
|
|
|
|
2023-03-21 14:38:07 -04:00
|
|
|
use crate::{diagnose::Annotate, diagnostic_panic};
|
|
|
|
|
tamer: Very basic support for template application NIR -> xmli
This this a big change that's difficult to break up, and I don't have the
energy after it.
This introduces nullary template application, short- and long-form. Note
that a body of the short form is a `@values@` argument, so that's not
supported yet.
This continues to formalize the idea of what "template application" and
"template expansion" mean in TAMER. It makes a separate `TplApply`
unnecessary, because now application is simply a reference to a
template. Expansion and application are one and the same: when a template
expands, it'll re-bind metavariables to the parent context. So in a
template context, this amounts to application.
But applying a closed template will have nothing to bind, and so is
equivalent to expansion. And since `Meta` objects are not valid outside of
a `Tpl` context, applying a non-closed template outside of another template
will be invalid.
So we get all of this with a single primitive (getting the "value" of a
template).
The expansion is conceptually like `,@` in Lisp, where we're splicing trees.
It's a mess in some spots, but I want to get this committed before I do a
little bit of cleanup.
2023-03-17 10:25:56 -04:00
|
|
|
// Single-item "queue".
|
|
|
|
if let Some(obj) = queue.take() {
|
|
|
|
return Transition(Ready).ok(obj).with_lookahead(tok);
|
2023-01-23 16:30:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
match (self, tok) {
|
2023-01-30 16:51:24 -05:00
|
|
|
(Ready, Nir::Open(NirEntity::Package, span)) => {
|
2023-03-15 10:59:22 -04:00
|
|
|
Transition(Ready).ok(Air::PkgStart(span))
|
2023-01-30 16:51:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
(Ready, Nir::Close(NirEntity::Package, span)) => {
|
2023-03-15 10:59:22 -04:00
|
|
|
Transition(Ready).ok(Air::PkgEnd(span))
|
2023-01-30 16:51:24 -05:00
|
|
|
}
|
|
|
|
|
2023-02-22 23:45:23 -05:00
|
|
|
(Ready, Nir::Open(NirEntity::Rate | NirEntity::Sum, span)) => {
|
2023-03-15 10:59:22 -04:00
|
|
|
Transition(Ready).ok(Air::ExprStart(ExprOp::Sum, span))
|
2023-01-23 16:30:25 -05:00
|
|
|
}
|
2023-02-24 15:55:25 -05:00
|
|
|
(Ready, Nir::Open(NirEntity::Product, span)) => {
|
2023-03-15 10:59:22 -04:00
|
|
|
Transition(Ready).ok(Air::ExprStart(ExprOp::Product, span))
|
2023-01-23 16:30:25 -05:00
|
|
|
}
|
2023-03-09 22:28:26 -05:00
|
|
|
(Ready, Nir::Open(NirEntity::Ceil, span)) => {
|
2023-03-15 10:59:22 -04:00
|
|
|
Transition(Ready).ok(Air::ExprStart(ExprOp::Ceil, span))
|
2023-03-09 22:28:26 -05:00
|
|
|
}
|
|
|
|
(Ready, Nir::Open(NirEntity::Floor, span)) => {
|
2023-03-15 10:59:22 -04:00
|
|
|
Transition(Ready).ok(Air::ExprStart(ExprOp::Floor, span))
|
2023-03-09 22:28:26 -05:00
|
|
|
}
|
2023-02-24 23:01:02 -05:00
|
|
|
(Ready, Nir::Open(NirEntity::Classify | NirEntity::All, span)) => {
|
2023-03-15 10:59:22 -04:00
|
|
|
Transition(Ready).ok(Air::ExprStart(ExprOp::Conj, span))
|
2023-02-24 23:01:02 -05:00
|
|
|
}
|
|
|
|
(Ready, Nir::Open(NirEntity::Any, span)) => {
|
2023-03-15 10:59:22 -04:00
|
|
|
Transition(Ready).ok(Air::ExprStart(ExprOp::Disj, span))
|
2023-02-24 23:01:02 -05:00
|
|
|
}
|
2023-01-23 16:30:25 -05:00
|
|
|
|
2023-02-28 15:31:49 -05:00
|
|
|
(Ready, Nir::Open(NirEntity::Tpl, span)) => {
|
2023-03-15 10:59:22 -04:00
|
|
|
Transition(Ready).ok(Air::TplStart(span))
|
2023-02-28 15:31:49 -05:00
|
|
|
}
|
|
|
|
(Ready, Nir::Close(NirEntity::Tpl, span)) => {
|
2023-03-15 10:59:22 -04:00
|
|
|
Transition(Ready).ok(Air::TplEnd(span))
|
2023-02-28 15:31:49 -05:00
|
|
|
}
|
|
|
|
|
tamer: Very basic support for template application NIR -> xmli
This this a big change that's difficult to break up, and I don't have the
energy after it.
This introduces nullary template application, short- and long-form. Note
that a body of the short form is a `@values@` argument, so that's not
supported yet.
This continues to formalize the idea of what "template application" and
"template expansion" mean in TAMER. It makes a separate `TplApply`
unnecessary, because now application is simply a reference to a
template. Expansion and application are one and the same: when a template
expands, it'll re-bind metavariables to the parent context. So in a
template context, this amounts to application.
But applying a closed template will have nothing to bind, and so is
equivalent to expansion. And since `Meta` objects are not valid outside of
a `Tpl` context, applying a non-closed template outside of another template
will be invalid.
So we get all of this with a single primitive (getting the "value" of a
template).
The expansion is conceptually like `,@` in Lisp, where we're splicing trees.
It's a mess in some spots, but I want to get this committed before I do a
little bit of cleanup.
2023-03-17 10:25:56 -04:00
|
|
|
(Ready, Nir::Open(NirEntity::TplApply(None), span)) => {
|
|
|
|
Transition(Ready).ok(Air::TplStart(span))
|
|
|
|
}
|
|
|
|
|
2023-03-21 14:38:07 -04:00
|
|
|
// Short-hand template application must be handled through
|
|
|
|
// desugaring as part of the lowering pipeline,
|
|
|
|
// so that it is converted to long form before getting here.
|
2023-03-22 10:07:16 -04:00
|
|
|
(
|
|
|
|
Ready,
|
|
|
|
Nir::Open(
|
|
|
|
NirEntity::TplApply(Some(_)) | NirEntity::TplParam(Some(_)),
|
|
|
|
span,
|
|
|
|
),
|
|
|
|
) => {
|
2023-03-21 14:38:07 -04:00
|
|
|
// TODO: In the future maybe TAMER will have evolved its
|
|
|
|
// abstractions enough that there's an ROI for prohibiting
|
|
|
|
// this at the type level.
|
|
|
|
diagnostic_panic!(
|
|
|
|
vec![
|
|
|
|
span.internal_error(
|
|
|
|
"attempted shorthand template application"
|
|
|
|
),
|
|
|
|
span.help(
|
|
|
|
"TAMER must be compiled with support for \
|
|
|
|
shorthand template application by utilizing the \
|
|
|
|
nir::tplshort module in the lowering pipeline."
|
|
|
|
)
|
|
|
|
],
|
|
|
|
"shortand template application is unsupported in this \
|
|
|
|
build of TAMER"
|
|
|
|
)
|
tamer: Very basic support for template application NIR -> xmli
This this a big change that's difficult to break up, and I don't have the
energy after it.
This introduces nullary template application, short- and long-form. Note
that a body of the short form is a `@values@` argument, so that's not
supported yet.
This continues to formalize the idea of what "template application" and
"template expansion" mean in TAMER. It makes a separate `TplApply`
unnecessary, because now application is simply a reference to a
template. Expansion and application are one and the same: when a template
expands, it'll re-bind metavariables to the parent context. So in a
template context, this amounts to application.
But applying a closed template will have nothing to bind, and so is
equivalent to expansion. And since `Meta` objects are not valid outside of
a `Tpl` context, applying a non-closed template outside of another template
will be invalid.
So we get all of this with a single primitive (getting the "value" of a
template).
The expansion is conceptually like `,@` in Lisp, where we're splicing trees.
It's a mess in some spots, but I want to get this committed before I do a
little bit of cleanup.
2023-03-17 10:25:56 -04:00
|
|
|
}
|
|
|
|
(Ready, Nir::Close(NirEntity::TplApply(_), span)) => {
|
|
|
|
Transition(Ready).ok(Air::TplEndRef(span))
|
|
|
|
}
|
|
|
|
|
2023-03-22 10:07:16 -04:00
|
|
|
(Ready, Nir::Open(NirEntity::TplParam(None), span)) => {
|
|
|
|
Transition(Ready).ok(Air::TplMetaStart(span))
|
|
|
|
}
|
|
|
|
(Ready, Nir::Close(NirEntity::TplParam(_), span)) => {
|
|
|
|
Transition(Ready).ok(Air::TplMetaEnd(span))
|
|
|
|
}
|
|
|
|
(Ready, Nir::Text(lexeme)) => {
|
|
|
|
Transition(Ready).ok(Air::TplLexeme(lexeme))
|
|
|
|
}
|
|
|
|
|
2023-02-24 15:55:25 -05:00
|
|
|
(
|
|
|
|
Ready,
|
|
|
|
Nir::Close(
|
2023-02-24 23:01:02 -05:00
|
|
|
NirEntity::Rate
|
|
|
|
| NirEntity::Sum
|
|
|
|
| NirEntity::Product
|
2023-03-09 22:28:26 -05:00
|
|
|
| NirEntity::Ceil
|
|
|
|
| NirEntity::Floor
|
2023-02-24 23:01:02 -05:00
|
|
|
| NirEntity::Classify
|
|
|
|
| NirEntity::All
|
|
|
|
| NirEntity::Any,
|
2023-02-24 15:55:25 -05:00
|
|
|
span,
|
|
|
|
),
|
2023-03-15 10:59:22 -04:00
|
|
|
) => Transition(Ready).ok(Air::ExprEnd(span)),
|
2023-02-24 15:55:25 -05:00
|
|
|
|
2023-01-23 16:30:25 -05:00
|
|
|
(Ready, Nir::BindIdent(spair)) => {
|
2023-02-28 11:31:06 -05:00
|
|
|
Transition(Ready).ok(Air::BindIdent(spair))
|
2023-01-23 16:30:25 -05:00
|
|
|
}
|
tamer: Very basic support for template application NIR -> xmli
This this a big change that's difficult to break up, and I don't have the
energy after it.
This introduces nullary template application, short- and long-form. Note
that a body of the short form is a `@values@` argument, so that's not
supported yet.
This continues to formalize the idea of what "template application" and
"template expansion" mean in TAMER. It makes a separate `TplApply`
unnecessary, because now application is simply a reference to a
template. Expansion and application are one and the same: when a template
expands, it'll re-bind metavariables to the parent context. So in a
template context, this amounts to application.
But applying a closed template will have nothing to bind, and so is
equivalent to expansion. And since `Meta` objects are not valid outside of
a `Tpl` context, applying a non-closed template outside of another template
will be invalid.
So we get all of this with a single primitive (getting the "value" of a
template).
The expansion is conceptually like `,@` in Lisp, where we're splicing trees.
It's a mess in some spots, but I want to get this committed before I do a
little bit of cleanup.
2023-03-17 10:25:56 -04:00
|
|
|
(Ready, Nir::Ref(spair)) => {
|
|
|
|
Transition(Ready).ok(Air::RefIdent(spair))
|
|
|
|
}
|
2023-01-23 16:30:25 -05:00
|
|
|
|
2023-03-22 10:07:16 -04:00
|
|
|
(Ready, Nir::Todo | Nir::TodoAttr(..) | Nir::Desc(..)) => {
|
|
|
|
Transition(Ready).ok(Air::Todo(UNKNOWN_SPAN))
|
|
|
|
}
|
2023-01-23 16:30:25 -05:00
|
|
|
}
|
2022-12-13 13:34:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn is_accepting(&self, _: &Self::Context) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
|
|
pub enum NirToAirError {
|
|
|
|
Todo,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for NirToAirError {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
|
|
use NirToAirError::*;
|
|
|
|
|
|
|
|
match self {
|
|
|
|
Todo => write!(f, "TODO"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Error for NirToAirError {}
|
|
|
|
|
|
|
|
impl Diagnostic for NirToAirError {
|
|
|
|
fn describe(&self) -> Vec<crate::diagnose::AnnotatedSpan> {
|
|
|
|
// TODO
|
|
|
|
vec![]
|
|
|
|
}
|
|
|
|
}
|
2023-01-23 16:30:25 -05:00
|
|
|
|
|
|
|
#[cfg(all(test, feature = "wip-nir-to-air"))]
|
|
|
|
mod test;
|