viirya commented on a change in pull request #1849: URL: https://github.com/apache/arrow-datafusion/pull/1849#discussion_r810586046
########## File path: datafusion-jit/src/ast.rs ########## @@ -0,0 +1,359 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use cranelift::codegen::ir; +use std::fmt::{Display, Formatter}; + +#[derive(Clone, Debug)] +/// Statement +pub enum Stmt { + /// if-then-else + IfElse(Box<Expr>, Vec<Stmt>, Vec<Stmt>), + /// while + WhileLoop(Box<Expr>, Vec<Stmt>), + /// assignment + Assign(String, Box<Expr>), + /// call function for side effect + Call(String, Vec<Expr>), + /// declare a new variable of type + Declare(String, JITType), +} + +#[derive(Copy, Clone, Debug)] +/// Shorthand typed literals +pub enum TypedLit { + Bool(bool), + Int(i64), + Float(f32), + Double(f64), +} + +#[derive(Clone, Debug)] +/// Expression +pub enum Expr { + /// literal + Literal(Literal), + /// variable + Identifier(String, JITType), + /// binary expression + Binary(BinaryExpr), + /// call function expression + Call(String, Vec<Expr>, JITType), +} + +impl Expr { + pub fn get_type(&self) -> JITType { + match self { + Expr::Literal(lit) => lit.get_type(), + Expr::Identifier(_, ty) => *ty, + Expr::Binary(bin) => bin.get_type(), + Expr::Call(_, _, ty) => *ty, + } + } +} + +impl Literal { + fn get_type(&self) -> JITType { + match self { + Literal::Parsing(_, ty) => *ty, + Literal::Typed(tl) => tl.get_type(), + } + } +} + +impl TypedLit { + fn get_type(&self) -> JITType { + match self { + TypedLit::Bool(_) => BOOL, + TypedLit::Int(_) => I64, + TypedLit::Float(_) => F32, + TypedLit::Double(_) => F64, + } + } +} + +impl BinaryExpr { + fn get_type(&self) -> JITType { + match self { + BinaryExpr::Eq(_, _) => BOOL, + BinaryExpr::Ne(_, _) => BOOL, + BinaryExpr::Lt(_, _) => BOOL, + BinaryExpr::Le(_, _) => BOOL, + BinaryExpr::Gt(_, _) => BOOL, + BinaryExpr::Ge(_, _) => BOOL, + BinaryExpr::Add(lhs, _) => lhs.get_type(), + BinaryExpr::Sub(lhs, _) => lhs.get_type(), + BinaryExpr::Mul(lhs, _) => lhs.get_type(), + BinaryExpr::Div(lhs, _) => lhs.get_type(), + } + } +} + +#[derive(Clone, Debug)] +/// Binary expression +pub enum BinaryExpr { + /// == + Eq(Box<Expr>, Box<Expr>), + /// != + Ne(Box<Expr>, Box<Expr>), + /// < + Lt(Box<Expr>, Box<Expr>), + /// <= + Le(Box<Expr>, Box<Expr>), + /// > + Gt(Box<Expr>, Box<Expr>), + /// >= + Ge(Box<Expr>, Box<Expr>), + /// add + Add(Box<Expr>, Box<Expr>), + /// subtract + Sub(Box<Expr>, Box<Expr>), + /// multiply + Mul(Box<Expr>, Box<Expr>), + /// divide + Div(Box<Expr>, Box<Expr>), +} + +#[derive(Clone, Debug)] +/// Literal +pub enum Literal { + /// Parsable literal with type + Parsing(String, JITType), + /// Shorthand literals of common types + Typed(TypedLit), +} + +#[derive(Copy, Clone, PartialEq, Eq, Hash)] +/// Type to be used in JIT +pub struct JITType { + /// The cranelift type + pub(crate) native: ir::Type, + /// re-expose inner field of `ir::Type` out for easier pattern matching + pub(crate) code: u8, Review comment: Is "re-expose inner field..." the doc for `code`? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
