radon-project / radon

The Radon Programming Language
https://radon-project.github.io
GNU General Public License v3.0
23 stars 2 forks source link

Added help() shell function #150

Closed Vardan2009 closed 4 months ago

Vardan2009 commented 4 months ago

help() displays help info for each data type

Part of #8

Almas-Ali commented 4 months ago

How this will work with user defined objects?

Vardan2009 commented 4 months ago

Didn't think of that. I think users can define a custom__help_repr__ function in their classes to return a string

Almas-Ali commented 4 months ago

Can we dynamically generate the help from object? Example:

class AClass {
    "AClass a simple OOP class."

    fun __contructor__(num1) {
        "The constructor"
        this.num1 = num1
    }

    fun squre() {
        # some hidden logics
        return this.num1 ^ 2
    }

    fun add(n1, n2) {
        # some hidden logics
        return n1 + n2
    }
}

Output

Help on int object:

class AClass()
 |  
 |  AClass a simple OOP class.
 | 
 | a = AClass()
 | 
 | null
 |  
 |  Methods defined here:
 |  
 |  squre()
 |      return this.num1 ^ 2
 |  
 |  add(n1, n2)
 |      return n1 + n2
 |  

Here in this example, help(obj) will generate documentation like this format for builtin classes and user defined classes. We need to manage this or similar syntax.

Vardan2009 commented 4 months ago

Can we dynamically generate the help from object?

Is there a way to get all the methods and what they return dynamically?

Almas-Ali commented 4 months ago

Can we dynamically generate the help from object?

Is there a way to get all the methods and what they return dynamically?

Not exists till now?

Vardan2009 commented 4 months ago

So when defining a method in a class, it can also store that info somewhere, that might also help making the dir function later

Almas-Ali commented 4 months ago

So when defining a method in a class, it can also store that info somewhere, that might also help making the dir function later

Yeah, you can create like that.

Almas-Ali commented 4 months ago

What do you think @angelcaru ?

Vardan2009 commented 4 months ago

I'm currently working on it, here is the result so far

Help on object Test:

class Test
| local_value = "value"
| fun __constructor__()
|        returns something
|
| fun test(test,s)
|        returns something

How can I check what the function returns if this is a dynamic typed language? Maybe we can put something else there, like a description for the method?

Almas-Ali commented 4 months ago

How can I check what the function returns if this is a dynamic typed language? Maybe we can put something else there, like a description for the method?

Showing description in return? Okay, fine for now.

Vardan2009 commented 4 months ago

how should the descriptions be written? Maybe a local variable in each function like:

fun test(test,s){
   __desc = "Description for method test"
   # Do something
}
angelcaru commented 4 months ago

Maybe a built-in function? Like:

fun test(test, s) {
    # Do something
}
set_doc(test, "Description for method test")

Because I don't see how you would get the value for a __desc variable without actually running the function

Vardan2009 commented 4 months ago

Oh... right

Almas-Ali commented 4 months ago

I think we can do it like Python. In the first line, if we have a string, then set it to description else put empty.

Vardan2009 commented 4 months ago

I think I got it working, here Input

class Test
{
    fun __constructor__(){

    }

    fun test(test,s){
        "Description for method test"
        print("tesst")
    }
}

help(Test())

Output

Help on object Test:

class Test
| fun __constructor__()
|        [No Description]
|
| fun test(test,s)
|        Description for method test
|
Almas-Ali commented 4 months ago

This will be like print(help(Test)) or print(help(Test()))

Vardan2009 commented 4 months ago

it will be just help(Test()), and yes, it always has to get the instance to the class, not the class itself

angelcaru commented 4 months ago

it always has to get the instance to the class, not the class itself

That seems inconvenient. What if the constructor has arguments?

Vardan2009 commented 4 months ago

Yeah... I will try to get it working to accept both instances and classes

angelcaru commented 4 months ago

It should also work on standalone functions. Right now it only works on classes

Vardan2009 commented 4 months ago

So, something like this?

Help on function test
| fun test(args)
|       desc
Almas-Ali commented 4 months ago

A new thing. If I want to get the output from help() it returns the object. But, it should return null.

fun some() -> null
a = help(some)
print(a) # <function some>
Almas-Ali commented 4 months ago

Little spacing will make this look pretty.

Help on object Test:

class Test
| property1 = "Hello, World!"
| fun __constructor__()
|   [No Description]
| fun func1(arg1, arg2 = true)
|   Description for func1
| fun func2(arg1 = null)
|   Description for func2
| fun no_description()
|   [No Description]

Help on object Test:

class Test
| property1 = "Hello, World!"
| fun __constructor__()
|   [No Description]
| fun func1(arg1, arg2 = true)
|   Description for func1
| fun func2(arg1 = null)
|   Description for func2
| fun no_description()
|   [No Description]

Help on function standalone
| fun standalone(arg1, arg2 = true)
|   This is a standalone function
Almas-Ali commented 4 months ago

Previous output was nice.

Vardan2009 commented 4 months ago

Is this good?

Help on object Test:

class Test
| property1 = "Hello, World!"
|
| fun __constructor__()
|       [No Description]
|
| fun func1(arg1, arg2)
|       Description for func1
|
| fun func2(arg1)
|       Description for func2
|
| fun no_description()
|       [No Description]
|

Help on object Test:

class Test
| property1 = "Hello, World!"
|
| fun __constructor__()
|       [No Description]
|
| fun func1(arg1, arg2)
|       Description for func1
|
| fun func2(arg1)
|       Description for func2
|
| fun no_description()
|       [No Description]
|

Help on function standalone:

| fun standalone(arg1, arg2)
|       This is a standalone function
Almas-Ali commented 4 months ago

Add spacing in also property. Example:

Help on object Test:

class Test
|
| property1 = "Hello, World!"
|
| fun __constructor__()
|       [No Description]
|
| fun func1(arg1, arg2)
|       Description for func1
|
| fun func2(arg1)
|       Description for func2
|
| fun no_description()
|       [No Description]
|

Help on object Test:

class Test
|
| property1 = "Hello, World!"
|
| fun __constructor__()
|       [No Description]
|
| fun func1(arg1, arg2)
|       Description for func1
|
| fun func2(arg1)
|       Description for func2
|
| fun no_description()
|       [No Description]
|
Almas-Ali commented 4 months ago

It is running the method or class to get its inner string!! This is now expected.

Vardan2009 commented 4 months ago

?

Almas-Ali commented 4 months ago

?

Check this. It is running the Test class to get it's inner descriptions.

class Test
{
    property1 = "Hello, World!"
    print(property1)
    val = 324 + true

    fun __constructor__()
    {

    }

    fun func1(arg1, arg2=true)
    {
        "Description for func1"
    }

    fun func2(arg1=null)
    {
        "Description for func2"
        return true
    }

    fun no_description() {
        return null
    }
}

help(Test)
angelcaru commented 4 months ago

No it's not? It's running the Test class to create the class. That's how it works

Vardan2009 commented 4 months ago

The descriptions get collected during the parsing. After the LBRACE, when it encounters a string, it sets it as it's description, then continues the parsing.