Skip to content

Commit

Permalink
Remove list indexing from MiniExpr (#257)
Browse files Browse the repository at this point in the history
I noticed that this was different from BYOND's behaviour where
`new x[y]` actually compiles as `(new x)[y]`. The same behaviour
applies for `new x?[y]` so I don't anticipate the code I am removing
being needed for that.

I confirmed that BYOND has been compiling it this way since at least
513.1490, so if it changed it was long ago.

[SpaceManiac] As far as I can tell, there was no BYOND change and this 
code was wrong all along.
  • Loading branch information
willox authored Mar 24, 2021
1 parent 7c2b921 commit 8c751bd
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 29 deletions.
18 changes: 16 additions & 2 deletions src/dreamchecker/tests/new_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ extern crate dreamchecker as dc;
use dc::test_helpers::*;

pub const NEW_DOT_ERRORS: &[(u32, u16, &str)] = &[
(13, 14, "got '(', expected one of: operator, field access, ';'"),
(12, 14, "got '(', expected one of: operator, field access, ';'"),
];

#[test]
Expand All @@ -20,11 +20,25 @@ fn new_dot() {
new /obj()
new /obj{name = "foo"}()
new .subtype()
new L[1]()
new src.name
new foo()()
new /obj[0]() // TODO: see parser.rs
new 2 + 2() // TODO: see parser.rs
"##.trim();
check_errors_match(code, NEW_DOT_ERRORS);
}

pub const NEW_PRECEDENCE_ERRORS: &[(u32, u16, &str)] = &[
(4, 13, "got '(', expected one of: operator, field access, ';'"),
];

#[test]
fn new_precedence() {
let code = r##"
/mob/subtype
/mob/proc/foo()
/mob/proc/test()
new L[1]()
"##.trim();
check_errors_match(code, NEW_PRECEDENCE_ERRORS);
}
21 changes: 8 additions & 13 deletions src/dreammaker/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ pub enum NewType {
/// A "mini-expression" in which to find the prefab to instantiate.
MiniExpr {
ident: Ident,
fields: Vec<IndexOrField>,
fields: Vec<Field>,
},
}

Expand Down Expand Up @@ -680,21 +680,16 @@ pub enum Follow {
Call(IndexKind, Ident, Vec<Expression>),
}

/// Like a `Follow` but supports index or fields only.
/// Like a `Follow` but only supports field accesses.
#[derive(Debug, Clone, PartialEq)]
pub enum IndexOrField {
/// Index the value by an expression.
Index(Box<Expression>),
/// Access a field of the value.
Field(IndexKind, Ident),
pub struct Field {
pub kind: IndexKind,
pub ident: Ident,
}

impl From<IndexOrField> for Follow {
fn from(input: IndexOrField) -> Follow {
match input {
IndexOrField::Index(expr) => Follow::Index(expr),
IndexOrField::Field(kind, name) => Follow::Field(kind, name),
}
impl From<Field> for Follow {
fn from(field: Field) -> Follow {
Follow::Field(field.kind, field.ident)
}
}

Expand Down
19 changes: 5 additions & 14 deletions src/dreammaker/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1890,15 +1890,14 @@ impl<'ctx, 'an, 'inp> Parser<'ctx, 'an, 'inp> {

let start = self.updated_location();
let term = match self.next("term")? {
// term :: 'new' (prefab | (ident (index | field)*))? arglist?
// term :: 'new' (prefab | (ident field*))? arglist?
Token::Ident(ref i, _) if i == "new" => {
// It's not entirely clear what is supposed to be valid here.
// Some things definitely are:
// * new()
// * new /obj()
// * new /obj{name = "foo"}()
// * new .relative/path()
// * new some_list[0]()
// * new various.field.accesses()
// But some things definitely aren't:
// * new some_proc()() - first parens belong to the 'new'
Expand All @@ -1921,7 +1920,7 @@ impl<'ctx, 'an, 'inp> Parser<'ctx, 'an, 'inp> {
} else if let Some(ident) = self.ident()? {
let mut fields = Vec::new();
let mut belongs_to = vec![ident.clone()];
while let Some(item) = self.index_or_field(&mut belongs_to, false)? {
while let Some(item) = self.field(&mut belongs_to, false)? {
fields.push(item);
}
NewType::MiniExpr { ident, fields }
Expand Down Expand Up @@ -2163,17 +2162,9 @@ impl<'ctx, 'an, 'inp> Parser<'ctx, 'an, 'inp> {
}

// TODO: somehow fix the fact that this is basically copy-pasted from
// follow() above, except for the very end.
fn index_or_field(&mut self, belongs_to: &mut Vec<Ident>, in_ternary: bool) -> Status<IndexOrField> {
// follow() above.
fn field(&mut self, belongs_to: &mut Vec<Ident>, in_ternary: bool) -> Status<Field> {
let kind = match self.next("field access")? {
// follow :: '[' expression ']'
Token::Punct(Punctuation::LBracket) => {
belongs_to.clear();
let expr = require!(self.expression());
require!(self.exact(Token::Punct(Punctuation::RBracket)));
return success(IndexOrField::Index(Box::new(expr)))
}

// follow :: '.' ident
// TODO: only apply these rules if there is no whitespace around the punctuation
Token::Punct(Punctuation::Dot) => IndexKind::Dot,
Expand Down Expand Up @@ -2204,7 +2195,7 @@ impl<'ctx, 'an, 'inp> Parser<'ctx, 'an, 'inp> {
self.annotate_precise(start..end, || Annotation::ScopedVar(belongs_to.clone(), ident.clone()));
belongs_to.push(ident.clone());
}
success(IndexOrField::Field(kind, ident))
success(Field { kind, ident })
}

/// a parenthesized, comma-separated list of expressions
Expand Down

0 comments on commit 8c751bd

Please sign in to comment.