Skip to content

Commit

Permalink
Switch everything away from UserVal
Browse files Browse the repository at this point in the history
  • Loading branch information
adamchalmers committed Nov 8, 2024
1 parent dcab187 commit 733048f
Show file tree
Hide file tree
Showing 15 changed files with 516 additions and 683 deletions.
49 changes: 19 additions & 30 deletions src/wasm-lib/kcl/src/ast/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub use crate::ast::types::{
use crate::{
docs::StdLibFn,
errors::KclError,
executor::{ExecState, ExecutorContext, KclValue, Metadata, SourceRange, TagIdentifier, UserVal},
executor::{ExecState, ExecutorContext, KclValue, Metadata, SourceRange, TagIdentifier},
parser::PIPE_OPERATOR,
std::kcl_stdlib::KclStdLibFn,
};
Expand Down Expand Up @@ -59,6 +59,14 @@ pub struct Node<T> {
pub module_id: ModuleId,
}

impl<T> Node<T> {
pub fn metadata(&self) -> Metadata {
Metadata {
source_range: SourceRange([self.start, self.end, self.module_id.0 as usize]),
}
}
}

impl<T: JsonSchema> schemars::JsonSchema for Node<T> {
fn schema_name() -> String {
T::schema_name()
Expand Down Expand Up @@ -1708,34 +1716,26 @@ impl Literal {

impl From<Node<Literal>> for KclValue {
fn from(literal: Node<Literal>) -> Self {
KclValue::UserVal(UserVal {
value: JValue::from(literal.value.clone()),
meta: vec![Metadata {
source_range: literal.into(),
}],
})
let meta = vec![literal.metadata()];
match literal.inner.value {
LiteralValue::IInteger(value) => KclValue::Int { value, meta },
LiteralValue::Fractional(value) => KclValue::Number { value, meta },
LiteralValue::String(value) => KclValue::String { value, meta },
LiteralValue::Bool(value) => KclValue::Bool { value, meta },
}
}
}

impl From<&Node<Literal>> for KclValue {
fn from(literal: &Node<Literal>) -> Self {
KclValue::UserVal(UserVal {
value: JValue::from(literal.value.clone()),
meta: vec![Metadata {
source_range: literal.into(),
}],
})
Self::from(literal.to_owned())
}
}

impl From<&BoxNode<Literal>> for KclValue {
fn from(literal: &BoxNode<Literal>) -> Self {
KclValue::UserVal(UserVal {
value: JValue::from(literal.value.clone()),
meta: vec![Metadata {
source_range: literal.into(),
}],
})
let b: &Node<Literal> = literal;
Self::from(b)
}
}

Expand Down Expand Up @@ -3010,17 +3010,6 @@ impl ConstraintLevels {
}
}

pub(crate) fn human_friendly_type(j: &JValue) -> &'static str {
match j {
JValue::Null => "null",
JValue::Bool(_) => "boolean (true/false value)",
JValue::Number(_) => "number",
JValue::String(_) => "string (text)",
JValue::Array(_) => "array (list)",
JValue::Object(_) => "object",
}
}

#[cfg(test)]
mod tests {
use pretty_assertions::assert_eq;
Expand Down
Loading

0 comments on commit 733048f

Please sign in to comment.