BREP_kernel 0.5.0

A boundary representation (BREP) geometry kernel for building CAD applications.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
//! A deterministic recursive-descent evaluator for the practical subset of the
//! app's expression DSL (user directive: move expressions to Rust).
//!
//! The previous app evaluated expressions with a JavaScript `Function()` reading a
//! `configurator` object and a `resolution` default (see
//! `PartHistory.buildExpressionSource`). The REAL expressions are arithmetic, so
//! this ports exactly that arithmetic surface — no arbitrary code eval. Anything outside
//! the supported grammar is a clear `Err`, never a silent wrong answer.
//!
//! Grammar (recursive descent, standard precedence):
//! ```text
//!   source      := statement*
//!   statement   := IDENT '=' expr ';'
//!   expr        := term (('+' | '-') term)*
//!   term        := unary (('*' | '/' | '%') unary)*
//!   unary       := ('-')? primary
//!   primary     := NUMBER
//!                | '(' expr ')'
//!                | dotted ('(' args ')')?
//!   dotted      := IDENT ('.' IDENT)*
//!   args        := (expr (',' expr)*)?
//! ```
//! Supported: f64 literals (incl. scientific notation), identifiers, `+ - * / %`,
//! unary `-`, parentheses, member access `configurator.field`, and a `Math.*`
//! subset: constants `PI`, `E`; unary `sin cos tan sqrt abs floor ceil round`;
//! `pow(a,b)`; variadic `min`/`max`. `//` line comments are skipped.

use std::collections::HashMap;

/// A value in the expression environment: a scalar, or the injected
/// `configurator` object (a flat `field -> f64` map, matching what the
/// `configurator = <values>` prelude exposes to expressions).
#[derive(Debug, Clone)]
enum EvalValue {
    Scalar(f64),
    Object(HashMap<String, f64>),
}

/// The expression environment: the variable bindings produced by evaluating the
/// prelude (`resolution = 32`), the injected `configurator` object, and the
/// user's `expressions` statements in order.
#[derive(Debug, Clone, Default)]
pub struct Env {
    vars: HashMap<String, EvalValue>,
    /// If the env failed to build, every `eval()` returns this error. Numeric
    /// literal params still pass through their own `param_f64` path without ever
    /// calling `eval`, so an expression-free history is unaffected by a poison.
    poison: Option<String>,
}

impl Env {
    /// Build the environment: prelude `resolution = 32`, then the injected
    /// `configurator` object, then the user's `expressions` statements in order
    /// (users may overwrite `resolution`). `configurator_json` may be the full
    /// configurator state (`{ values: {...}, ... }`) or a bare values map — the
    /// `.values` sub-object is used when present, matching the prelude.
    pub fn build(expressions: &str, configurator_json: &serde_json::Value) -> Result<Env, String> {
        let mut env = Env::default();
        env.vars
            .insert("resolution".to_string(), EvalValue::Scalar(32.0));
        env.vars.insert(
            "configurator".to_string(),
            EvalValue::Object(configurator_values(configurator_json)),
        );
        env.run_statements(expressions)?;
        Ok(env)
    }

    /// A poisoned environment: `eval()` reports `msg`; numeric params still pass
    /// through. Lets `execute_history` keep the `-> HistoryResult` signature and
    /// surface an expression-block error per-feature instead of globally.
    pub fn poisoned(msg: String) -> Env {
        Env {
            poison: Some(msg),
            ..Env::default()
        }
    }

    /// Evaluate a single expression string against the environment.
    pub fn eval(&self, source: &str) -> Result<f64, String> {
        if let Some(poison) = &self.poison {
            return Err(format!("expression environment failed to build: {poison}"));
        }
        let tokens = tokenize(source)?;
        let mut parser = Parser::new(&tokens, self);
        let value = parser.parse_expr()?;
        parser.expect_eof()?;
        Ok(value)
    }

    /// Lookup a bound scalar variable (test/introspection helper).
    pub fn get(&self, name: &str) -> Option<f64> {
        match self.vars.get(name) {
            Some(EvalValue::Scalar(value)) => Some(*value),
            _ => None,
        }
    }

    fn run_statements(&mut self, source: &str) -> Result<(), String> {
        let tokens = tokenize(source)?;
        let mut index = 0;
        while index < tokens.len() && tokens[index] != Token::Eof {
            // statement := IDENT '=' expr ';'
            let name = match &tokens[index] {
                Token::Ident(name) => name.clone(),
                other => {
                    return Err(format!(
                        "expected an identifier at the start of a statement, found {other:?}"
                    ))
                }
            };
            index += 1;
            if tokens.get(index) != Some(&Token::Assign) {
                return Err(format!("expected '=' after `{name}` in expression statement"));
            }
            index += 1;
            // Collect the RHS tokens up to the next ';'.
            let start = index;
            while index < tokens.len()
                && tokens[index] != Token::Semi
                && tokens[index] != Token::Eof
            {
                index += 1;
            }
            let rhs = &tokens[start..index];
            let mut parser = Parser::new(rhs, self);
            let value = parser.parse_expr()?;
            parser.expect_eof()?;
            self.vars.insert(name, EvalValue::Scalar(value));
            // Consume the terminating ';' if present.
            if tokens.get(index) == Some(&Token::Semi) {
                index += 1;
            }
        }
        Ok(())
    }
}

/// Extract the flat `field -> f64` values map from a configurator JSON value,
/// preferring a `.values` sub-object (the normalized configurator state shape).
/// Non-numeric fields are dropped (an expression referencing one errors clearly
/// at member-access time).
fn configurator_values(configurator: &serde_json::Value) -> HashMap<String, f64> {
    let source = configurator
        .get("values")
        .filter(|value| value.is_object())
        .unwrap_or(configurator);
    let mut map = HashMap::new();
    if let Some(object) = source.as_object() {
        for (key, value) in object {
            if let Some(number) = value_as_f64(value) {
                map.insert(key.clone(), number);
            }
        }
    }
    map
}

fn value_as_f64(value: &serde_json::Value) -> Option<f64> {
    match value {
        serde_json::Value::Number(number) => number.as_f64(),
        serde_json::Value::String(text) => text.trim().parse::<f64>().ok(),
        serde_json::Value::Bool(flag) => Some(if *flag { 1.0 } else { 0.0 }),
        _ => None,
    }
}

// ---------------------------------------------------------------------------
// Tokenizer
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, PartialEq)]
enum Token {
    Number(f64),
    Ident(String),
    Plus,
    Minus,
    Star,
    Slash,
    Percent,
    LParen,
    RParen,
    Dot,
    Comma,
    Assign,
    Semi,
    Eof,
}

fn tokenize(source: &str) -> Result<Vec<Token>, String> {
    let bytes = source.as_bytes();
    let mut tokens = Vec::new();
    let mut i = 0;
    while i < bytes.len() {
        let c = bytes[i] as char;
        if c.is_whitespace() {
            i += 1;
            continue;
        }
        // `//` line comment.
        if c == '/' && i + 1 < bytes.len() && bytes[i + 1] as char == '/' {
            while i < bytes.len() && bytes[i] as char != '\n' {
                i += 1;
            }
            continue;
        }
        match c {
            '+' => {
                tokens.push(Token::Plus);
                i += 1;
            }
            '-' => {
                tokens.push(Token::Minus);
                i += 1;
            }
            '*' => {
                tokens.push(Token::Star);
                i += 1;
            }
            '/' => {
                tokens.push(Token::Slash);
                i += 1;
            }
            '%' => {
                tokens.push(Token::Percent);
                i += 1;
            }
            '(' => {
                tokens.push(Token::LParen);
                i += 1;
            }
            ')' => {
                tokens.push(Token::RParen);
                i += 1;
            }
            '.' if !next_is_digit(bytes, i + 1) => {
                tokens.push(Token::Dot);
                i += 1;
            }
            ',' => {
                tokens.push(Token::Comma);
                i += 1;
            }
            '=' => {
                tokens.push(Token::Assign);
                i += 1;
            }
            ';' => {
                tokens.push(Token::Semi);
                i += 1;
            }
            _ if c.is_ascii_digit() || c == '.' => {
                let (number, next) = scan_number(bytes, i)?;
                tokens.push(Token::Number(number));
                i = next;
            }
            _ if c.is_ascii_alphabetic() || c == '_' || c == '$' => {
                let start = i;
                while i < bytes.len() {
                    let ch = bytes[i] as char;
                    if ch.is_ascii_alphanumeric() || ch == '_' || ch == '$' {
                        i += 1;
                    } else {
                        break;
                    }
                }
                tokens.push(Token::Ident(source[start..i].to_string()));
            }
            _ => return Err(format!("unexpected character `{c}` in expression")),
        }
    }
    tokens.push(Token::Eof);
    Ok(tokens)
}

fn next_is_digit(bytes: &[u8], i: usize) -> bool {
    i < bytes.len() && (bytes[i] as char).is_ascii_digit()
}

/// Scan a decimal number with optional fraction and scientific exponent.
fn scan_number(bytes: &[u8], start: usize) -> Result<(f64, usize), String> {
    let mut i = start;
    while next_is_digit(bytes, i) {
        i += 1;
    }
    if i < bytes.len() && bytes[i] as char == '.' {
        i += 1;
        while next_is_digit(bytes, i) {
            i += 1;
        }
    }
    if i < bytes.len() && (bytes[i] as char == 'e' || bytes[i] as char == 'E') {
        let mut j = i + 1;
        if j < bytes.len() && (bytes[j] as char == '+' || bytes[j] as char == '-') {
            j += 1;
        }
        if next_is_digit(bytes, j) {
            i = j;
            while next_is_digit(bytes, i) {
                i += 1;
            }
        }
    }
    let text = std::str::from_utf8(&bytes[start..i]).map_err(|error| error.to_string())?;
    let value = text
        .parse::<f64>()
        .map_err(|_| format!("invalid number literal `{text}`"))?;
    Ok((value, i))
}

// ---------------------------------------------------------------------------
// Parser + evaluator (evaluated directly against a fixed env)
// ---------------------------------------------------------------------------

struct Parser<'a> {
    tokens: &'a [Token],
    pos: usize,
    env: &'a Env,
}

impl<'a> Parser<'a> {
    fn new(tokens: &'a [Token], env: &'a Env) -> Self {
        Self {
            tokens,
            pos: 0,
            env,
        }
    }

    fn peek(&self) -> &Token {
        self.tokens.get(self.pos).unwrap_or(&Token::Eof)
    }

    fn advance(&mut self) -> Token {
        let token = self.tokens.get(self.pos).cloned().unwrap_or(Token::Eof);
        self.pos += 1;
        token
    }

    fn expect_eof(&self) -> Result<(), String> {
        match self.peek() {
            Token::Eof => Ok(()),
            other => Err(format!("unexpected trailing token {other:?} in expression")),
        }
    }

    fn parse_expr(&mut self) -> Result<f64, String> {
        let mut value = self.parse_term()?;
        loop {
            match self.peek() {
                Token::Plus => {
                    self.advance();
                    value += self.parse_term()?;
                }
                Token::Minus => {
                    self.advance();
                    value -= self.parse_term()?;
                }
                _ => break,
            }
        }
        Ok(value)
    }

    fn parse_term(&mut self) -> Result<f64, String> {
        let mut value = self.parse_unary()?;
        loop {
            match self.peek() {
                Token::Star => {
                    self.advance();
                    value *= self.parse_unary()?;
                }
                Token::Slash => {
                    self.advance();
                    value /= self.parse_unary()?;
                }
                Token::Percent => {
                    self.advance();
                    value %= self.parse_unary()?;
                }
                _ => break,
            }
        }
        Ok(value)
    }

    fn parse_unary(&mut self) -> Result<f64, String> {
        if self.peek() == &Token::Minus {
            self.advance();
            return Ok(-self.parse_unary()?);
        }
        if self.peek() == &Token::Plus {
            self.advance();
            return self.parse_unary();
        }
        self.parse_primary()
    }

    fn parse_primary(&mut self) -> Result<f64, String> {
        match self.advance() {
            Token::Number(value) => Ok(value),
            Token::LParen => {
                let value = self.parse_expr()?;
                if self.advance() != Token::RParen {
                    return Err("expected ')'".to_string());
                }
                Ok(value)
            }
            Token::Ident(first) => {
                let mut parts = vec![first];
                while self.peek() == &Token::Dot {
                    self.advance();
                    match self.advance() {
                        Token::Ident(name) => parts.push(name),
                        other => {
                            return Err(format!("expected identifier after '.', found {other:?}"))
                        }
                    }
                }
                if self.peek() == &Token::LParen {
                    self.advance();
                    let args = self.parse_args()?;
                    self.eval_call(&parts.join("."), &args)
                } else {
                    self.eval_reference(&parts)
                }
            }
            other => Err(format!("unexpected token {other:?} in expression")),
        }
    }

    fn parse_args(&mut self) -> Result<Vec<f64>, String> {
        let mut args = Vec::new();
        if self.peek() == &Token::RParen {
            self.advance();
            return Ok(args);
        }
        loop {
            args.push(self.parse_expr()?);
            match self.advance() {
                Token::Comma => continue,
                Token::RParen => break,
                other => return Err(format!("expected ',' or ')' in argument list, found {other:?}")),
            }
        }
        Ok(args)
    }

    fn eval_reference(&self, parts: &[String]) -> Result<f64, String> {
        match parts.len() {
            1 => match self.env.vars.get(&parts[0]) {
                Some(EvalValue::Scalar(value)) => Ok(*value),
                Some(EvalValue::Object(_)) => {
                    Err(format!("`{}` is an object, not a number", parts[0]))
                }
                None => Err(format!("unknown identifier `{}`", parts[0])),
            },
            2 => {
                if parts[0] == "Math" {
                    return match parts[1].as_str() {
                        "PI" => Ok(std::f64::consts::PI),
                        "E" => Ok(std::f64::consts::E),
                        other => Err(format!("unsupported Math constant `Math.{other}`")),
                    };
                }
                match self.env.vars.get(&parts[0]) {
                    Some(EvalValue::Object(map)) => map
                        .get(&parts[1])
                        .copied()
                        .ok_or_else(|| format!("`{}.{}` is not a number", parts[0], parts[1])),
                    Some(EvalValue::Scalar(_)) => {
                        Err(format!("`{}` is a number, not an object", parts[0]))
                    }
                    None => Err(format!("unknown identifier `{}`", parts[0])),
                }
            }
            _ => Err(format!("unsupported member access `{}`", parts.join("."))),
        }
    }

    fn eval_call(&self, name: &str, args: &[f64]) -> Result<f64, String> {
        let expect = |arity: usize| -> Result<(), String> {
            if args.len() == arity {
                Ok(())
            } else {
                Err(format!(
                    "`{name}` expects {arity} argument(s), got {}",
                    args.len()
                ))
            }
        };
        match name {
            "Math.sin" => {
                expect(1)?;
                Ok(args[0].sin())
            }
            "Math.cos" => {
                expect(1)?;
                Ok(args[0].cos())
            }
            "Math.tan" => {
                expect(1)?;
                Ok(args[0].tan())
            }
            "Math.sqrt" => {
                expect(1)?;
                Ok(args[0].sqrt())
            }
            "Math.abs" => {
                expect(1)?;
                Ok(args[0].abs())
            }
            "Math.floor" => {
                expect(1)?;
                Ok(args[0].floor())
            }
            "Math.ceil" => {
                expect(1)?;
                Ok(args[0].ceil())
            }
            "Math.round" => {
                expect(1)?;
                // JavaScript Math.round rounds half up (toward +inf); Rust round() is
                // half-away-from-zero. Match JavaScript.
                Ok((args[0] + 0.5).floor())
            }
            "Math.pow" => {
                expect(2)?;
                Ok(args[0].powf(args[1]))
            }
            "Math.min" => {
                if args.is_empty() {
                    return Ok(f64::INFINITY);
                }
                Ok(args.iter().copied().fold(f64::INFINITY, f64::min))
            }
            "Math.max" => {
                if args.is_empty() {
                    return Ok(f64::NEG_INFINITY);
                }
                Ok(args.iter().copied().fold(f64::NEG_INFINITY, f64::max))
            }
            other => Err(format!("unsupported function `{other}`")),
        }
    }
}

// BREP private tests: 6c9ff62a18f24739