Skip to content

Hacktoberfest Opportunity #8

@ali77gh

Description

@ali77gh

Builtin function

It's really easy to add a new builtin function to Chap.

Ideas

You can add what ever you think is cool but here is some ideas:

  1. builtin_function/bool we can have xor , shift_right , shift_left
  2. builtin_function/strings we can have char_at so we can loop over chars, to_list list is also cool (which converts string to list of chars) Add some string functions in built-in functions #16)
  3. builtin_function/list we can have length , join and clear Builtin functions (list) #15)

How to add

  1. Make sure you create new Issue for it and explain your Idea before you start

  2. Make a file in src/builtin_function/<module_name>/<function_name>.rs

  3. This is equal function and I want to explain how it works:

// imports you need
use crate::builtin_function::utils::{param_to_datatype, returns};
use crate::common::data_type::DataType;
use crate::common::errors::Result;
use crate::{common::executable::ExecutableLine, runtime::Runtime};

// do not change function signature, Just change the name
pub fn equal(runtime: &mut Runtime, executable: &ExecutableLine) -> Result<()> {
   // this is how you can get params
    let p1 = param_to_datatype(runtime, executable.params.first(), executable.line_number)?;
    let p2 = param_to_datatype(runtime, executable.params.get(1), executable.line_number)?;
    // for example in `5, 6 -> equal -> $output`
    // p1 is 5 and p2 is 6

    // `returns` function puts third param in $output variable  
    returns(runtime, executable, DataType::Bool(p1 == p2))
}
  1. go to here and add your function to functions match case

  2. Send PR to dev branch and tag me for review and If you need help just let me know

Make an error:

return Err(ChapError::runtime_with_msg(
    executable.line_number,
    "random_bool not supported in wasm".to_string(),
));

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions