2
2

More than 5 years have passed since last update.

【HSP】dupptrを使った構造体のような何か

Last updated at Posted at 2019-03-22

【上級者向け】プロのHSPerになりたけりゃdupptrを使え

概要

上記記事を見て突発的に思いついたことを実践しただけの記事。

#runtime "hsp3cl"

#const global str_t 2
#const global int_t 4 
#define global ctype Person(%1) %tPerson \
    %i=varptr(%1) :\
    dupptr %1_name,%p,16,str_t :\
    dupptr %1_age,%o+16,4,int_t
#define global ctype new@Person(%1,%2,%3) \
    sdim %1,20 :\
    Person(%1) :\
    %1_name=%2 :\
    %1_age=%3

#module
    #deffunc view var p
        Person(p)
        mes "名前:"+p_name
        mes "年齢:"+p_age
    return
#global

#module Program
    #deffunc main
        new@Person(student,"bob",13)
        view student
        new@Person(teacher,"john",30)
        view teacher
    return  
#global
main

関数の引数として複数型の複数変数をまとめて渡せるのは素晴らしい。
都度ポインタの定義が必要になるけど、そのまま変数引き継げるなら十分。
モジュール変数みたいにアクセサが必要にならないのは便利。

定義時に変数サイズを細かく指定しておかなきゃならないのが若干ネックになりそうか。

おまけ:モジュール変数版/列挙体&文字列配列版

#runtime "hsp3cl"

#module Person __name,__age
    #modcfunc name
        return __name
    #modfunc set_name str _name
        __name=_name: return
    #modcfunc age
        return __age
    #modfunc set_age int _age
        __age=_age: return

    #define new(%1,%2,%3) dimtype %1,5: newmod %1,Person,%2,%3
    #modinit str _name,int _age
        __name=_name
        __age=_age
    return
#global

#module
    #deffunc view var p
        mes "名前:"+name(p)
        mes "年齢:"+age(p)
    return
#global

#module Program
    #deffunc main
        new@Person student,"bob",13
        view student
        new@Person teacher,"john",30
        view teacher
    return  
#global
main
#runtime "hsp3cl"

#enum global name=0
#enum global age
#enum global PersonLen
#module Person
    #deffunc local initPerson array p,str _name,int _age
        p.name=_name
        p.age=str(_age)
    return
    #define new(%1,%2,%3) dim %1,PersonLen: initPerson@Person %1,%2,%3
#global

#module
    #deffunc view array p
        mes "名前:"+p.name
        mes "年齢:"+p.age
    return
#global

#module Program
    #deffunc main
        new@Person student,"bob",13
        view student
        new@Person teacher,"john",30
        view teacher
    return  
#global
main
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2