|
3 | 3 | use std::rc::Rc; |
4 | 4 |
|
5 | 5 | use crate::{ |
6 | | - ast::{Expr, ExprS}, |
| 6 | + ast::{Expr, ExprS, IdentifierKind}, |
7 | 7 | builtins::{BuiltinFn, BuiltinFns}, |
8 | 8 | errors::{ |
9 | 9 | CompileError::{self, WrongNumberOfArgs}, |
@@ -357,78 +357,60 @@ fn compile_expr( |
357 | 357 | } |
358 | 358 | } |
359 | 359 | Expr::Identifier(identifier) => { |
360 | | - let identifier_name = identifier.0.as_str(); |
361 | | - |
362 | | - if let Some((_, index)) = env.get_builtin_index(identifier_name) { |
363 | | - codes.push(GET); |
364 | | - codes.push(lookup::BUILTIN); |
365 | | - codes.push(index); |
366 | | - } else if let Some((_, index)) = env.get_user_builtin_index(identifier_name) { |
367 | | - codes.push(GET); |
368 | | - codes.push(lookup::USER_BUILTIN); |
369 | | - codes.push(index); |
370 | | - } else { |
371 | | - let identifier_prefix = &identifier_name[..1]; |
372 | | - let identifier_suffix = &identifier_name[1..]; |
373 | | - |
374 | | - match identifier_prefix { |
375 | | - "?" => { |
376 | | - if let Some(index) = get(&env.prompts, identifier_suffix) { |
377 | | - codes.push(GET); |
378 | | - codes.push(lookup::PROMPT); |
379 | | - codes.push(index); |
380 | | - } else { |
381 | | - errs.push(( |
382 | | - CompileError::Undefined(identifier_name.to_string()).into(), |
383 | | - span.clone(), |
384 | | - )); |
385 | | - } |
386 | | - } |
387 | | - "!" => { |
388 | | - if let Some(index) = get(&env.secrets, identifier_suffix) { |
389 | | - codes.push(GET); |
390 | | - codes.push(lookup::SECRET); |
391 | | - codes.push(index); |
392 | | - } else { |
393 | | - errs.push(( |
394 | | - CompileError::Undefined(identifier_name.to_string()).into(), |
395 | | - span.clone(), |
396 | | - )); |
397 | | - } |
398 | | - } |
399 | | - ":" => { |
400 | | - if let Some(index) = get(&env.vars, identifier_suffix) { |
401 | | - codes.push(GET); |
402 | | - codes.push(lookup::VAR); |
403 | | - codes.push(index); |
404 | | - } else { |
405 | | - errs.push(( |
406 | | - CompileError::Undefined(identifier_name.to_string()).into(), |
407 | | - span.clone(), |
408 | | - )); |
409 | | - } |
410 | | - } |
411 | | - "@" => { |
412 | | - if let Some(index) = get(&env.client_context, identifier_suffix) { |
413 | | - codes.push(GET); |
414 | | - codes.push(lookup::CLIENT_CTX); |
415 | | - codes.push(index); |
416 | | - } else { |
417 | | - errs.push(( |
418 | | - CompileError::Undefined(identifier_name.to_string()).into(), |
419 | | - span.clone(), |
420 | | - )); |
421 | | - } |
422 | | - } |
423 | | - _ => { |
424 | | - errs.push(( |
425 | | - ExprError::CompileError(CompileError::Undefined( |
426 | | - identifier_name.to_string(), |
427 | | - )), |
428 | | - span.clone(), |
429 | | - )); |
| 360 | + let identifier_lookup_name = identifier.lookup_name(); |
| 361 | + let identifier_name = identifier.full_name().to_string(); |
| 362 | + |
| 363 | + let identifier_undefined_err = ( |
| 364 | + CompileError::Undefined(identifier_name).into(), |
| 365 | + span.clone(), |
| 366 | + ); |
| 367 | + |
| 368 | + let result = match identifier.identifier_kind() { |
| 369 | + IdentifierKind::Var => get(&env.vars, identifier_lookup_name).map(|index| { |
| 370 | + codes.push(GET); |
| 371 | + codes.push(lookup::VAR); |
| 372 | + codes.push(index); |
| 373 | + }), |
| 374 | + IdentifierKind::Prompt => get(&env.prompts, identifier_lookup_name).map(|index| { |
| 375 | + codes.push(GET); |
| 376 | + codes.push(lookup::PROMPT); |
| 377 | + codes.push(index); |
| 378 | + }), |
| 379 | + IdentifierKind::Secret => get(&env.secrets, identifier_lookup_name).map(|index| { |
| 380 | + codes.push(GET); |
| 381 | + codes.push(lookup::SECRET); |
| 382 | + codes.push(index); |
| 383 | + }), |
| 384 | + IdentifierKind::Client => { |
| 385 | + get(&env.client_context, identifier_lookup_name).map(|index| { |
| 386 | + codes.push(GET); |
| 387 | + codes.push(lookup::CLIENT_CTX); |
| 388 | + codes.push(index); |
| 389 | + }) |
| 390 | + } |
| 391 | + IdentifierKind::Builtin => { |
| 392 | + if let Some((_, index)) = env.get_builtin_index(identifier_lookup_name) { |
| 393 | + codes.push(GET); |
| 394 | + codes.push(lookup::BUILTIN); |
| 395 | + codes.push(index); |
| 396 | + |
| 397 | + Some(()) |
| 398 | + } else if let Some((_, index)) = |
| 399 | + env.get_user_builtin_index(identifier_lookup_name) |
| 400 | + { |
| 401 | + codes.push(GET); |
| 402 | + codes.push(lookup::USER_BUILTIN); |
| 403 | + codes.push(index); |
| 404 | + |
| 405 | + Some(()) |
| 406 | + } else { |
| 407 | + None |
430 | 408 | } |
431 | | - }; |
| 409 | + } |
| 410 | + }; |
| 411 | + |
| 412 | + if let None = result { |
| 413 | + errs.push(identifier_undefined_err); |
432 | 414 | } |
433 | 415 | } |
434 | 416 | Expr::Call(expr_call) => { |
|
0 commit comments